开发者

function overriding, onload precedence

开发者 https://www.devze.com 2023-03-31 09:50 出处:网络
I\'m developing some library and created this buggy code: //------------------- Gmaps 开发者_如何学Go= {};

I'm developing some library and created this buggy code:

//-------------------
Gmaps 开发者_如何学Go= {};
Gmaps.map = new Gmaps4RailsGoogle(); //there exists a default callback function in the created object
function load_map() {
  Gmaps.map.callback();
};
window.onload = load_map();
//--------------------
Gmaps.map.callback = function(){ alert('ok'); }

I thought, because the whole page is loaded, that callback would have been changed and alert message displayed.

But it's not the case and I haven't any error message in firebug.

If I then execute Gmaps.map.callback() in console, it works fine.

Is there any reason why the callback isn't overriden?

For context sake, code between --------- is created by the library but developers would be able to override some functions in his html.


You're not executing load_map onload. You're executing it immediately here:

window.onload = load_map();

and storing it's return value inside window.onload, so nothing is happening onload. Just change that line to:

window.onload = load_map;
0

精彩评论

暂无评论...
验证码 换一张
取 消