views:

243

answers:

4

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 fix it?

+2  A: 

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();
});
Nick Craver
This has absolutely nothing to do with the question..
Sean Kinsey
@Sean - It does if the `onload` handler is getting overridden or changed in any way, attaching a handler in this method would resolve that....this is *far* more likely than jQuery interfering with his object.
Nick Craver
@Nick But why would including jQuery do such a thing? But I know what your'e getting at.
Sean Kinsey
@Sean - It wouldn't out of the box, but I doubt the poster has simply added jQuery, probably either creates a javascript error overall, or there actually is some code the OP is running and the question isn't complete, which is the more common case on SO.
Nick Craver
+1  A: 

Just a side note.

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

// When the page has completely loaded
<body onload="someFunction()">
Kevin
+1  A: 

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.

Sean Kinsey
+1  A: 

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

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

It is simple, and it is easy.

Starx