开发者

jQuery conflict with body onload event

开发者 https://www.devze.com 2022-12-31 08:27 出处:网络
I have a strange conflict in my code. I have a function that called from body onload: var someGlobalVar=new SpecialType();

I have a strange conflict in my code.

I have a function that called from body onload:

var someGlobalVar=new SpecialType(); 
function OnBodyLoad()
{
  someGlobalVar.Bind();
}

But when I include jQuery 1.4.2 in my project I get an error that someGlobalVar is undefined. Why is the global variable undefined now, and what ways are there to f开发者_高级运维ix it?


Unless you need to use <body onload='OnBodyLoad()'> anymore, you can change thise to use jQuery's document.ready (and move it to an external file!) like this:

var someGlobalVar=new SpecialType(); 
$(OnBodyLoad);
//or..
$(function() {
  //other stuff..
  OnBodyLoad();
});
//or...
$(document).ready(function() {
  //other stuff..
  OnBodyLoad();
});


Why don't you use jQuery's load event?

$(window).load(function() {
      functiontoexecute();
});

It is simple, and it is easy.


Just a side note.

// DOM Ready
$(document).ready(function() {});

// When the page has completely loaded
<body onload="someFunction()">


Perhaps jQuery interferes with SpecialType and so the call to new SpecialType(); results in the variable someGlobalVar being undefined.

Try using the console to check for any warnings, and try to instantiate a SpecialType object manually. This should give you some insight.

0

精彩评论

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