July 5, 2010

jQuery conflict with body onload event

Question by Evgeny

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?

Answer by Kevin

Just a side note.

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

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

Answer by Starx

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

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

It is simple, and it is easy.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!