Jump to content

How do I wait for javascript file to be fully loaded ?


megetron

Recommended Posts

Hi,

I need to wait for a javascript file to be loaded by the client browser before executing rest of the script.

(I dont have access to the HEAD tag, because the script I develop is being called from other users page).

 

 

Here is what I am trying to do without success:

	echo "document.write(\"<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>"
		 ."<link rel='stylesheet' href='http://domain.com/css/style.css' type='text/css' media='screen'/>\");";

	 echo "document.write(\"<script>var newScript; newScript = document.createElement('script'); "
		   ."newScript.type = 'text/javascript'; "
		   ."newScript.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; "
		   ."document.getElementsByTagName('head')[0].appendChild(newScript);</script> \");";


	sleep(5);

	$list = "<link rel='stylesheet' href='http://domain.com/style.css' type='text/css' media='screen'/><p id='last'></p><div id='slidebox'>"
        ."<a class='close'></a>"
        ."<p>TITLE</p>"
        ."<h2>DESC</h2>"
        ."</div>"
        

	."<script type='text/javascript'>"
	."$(function() {"
		."$(window).scroll(function(){"
			."var distanceTop = $('#last').offset().top - $(window).height();"
			."if  ($(window).scrollTop() > distanceTop)"
				."$('#slidebox').animate({'right':'0px'},300);"
			."else "
				."$('#slidebox').stop(true).animate({'right':'-430px'},100);	"
		."});"
		."$('#slidebox .close').bind('click',function(){"
			."$(this).parent().remove();"
		."});"
	."});</script>";




	echo "document.write(\"$list\");";
               exit;

Why are you trying to mess around with document.write?  Just echo your tags as HTML.

 

echo "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>"
 ."<link rel='stylesheet' href='http://domain.com/css/style.css' type='text/css' media='screen'/>";		 

$list = "<p id='last'></p><div id='slidebox'>"
        ."<a class='close'></a>"
        ."<p>TITLE</p>"
        ."<h2>DESC</h2>"
        ."</div>"
."<script type='text/javascript'>"
."$(function() {"
."$(window).scroll(function(){"
	."var distanceTop = $('#last').offset().top - $(window).height();"
	."if  ($(window).scrollTop() > distanceTop)"
	."$('#slidebox').animate({'right':'0px'},300);"
	."else "
	."$('#slidebox').stop(true).animate({'right':'-430px'},100);	"
."});"
."$('#slidebox .close').bind('click',function(){"
	."$(this).parent().remove();"
."});"
."});</script>";

echo $list;

 

 

There is no need to do anything regarding waiting for the file to load.  The browser will automatically delay the execution of the second script block until the script is loaded.

 

here is how I call the script above:

<body>
<script language="javascript" type="text/javascript" src="http://domain.com/r.php?id=154"></script> 
</body>

but it doesn't work.

 

when I change the code to that:


<body>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>
<script language="javascript" type="text/javascript" src="http://domain.com/r.php?id=154"></script> 
</body>

 

the script works fine....

 

 

 

when I change the code to that:


<body>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>
<script language="javascript" type="text/javascript" src="http://domain.com/r.php?id=154"></script> 
</body>

 

the script works fine....

Of course you have to add the first line, your code has jquery in it and won't work unless jquery is loaded by the user. You could include something like this at the beginning of your code:

if (typeof jQuery == 'undefined') {  
    document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></scr' + 'ipt>');
}

That will detect if jQuery is already loaded, and load it if it isn't.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.