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;

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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....

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.