megetron Posted April 25, 2012 Share Posted April 25, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/261577-how-do-i-wait-for-javascript-file-to-be-fully-loaded/ Share on other sites More sharing options...
kicken Posted April 25, 2012 Share Posted April 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261577-how-do-i-wait-for-javascript-file-to-be-fully-loaded/#findComment-1340389 Share on other sites More sharing options...
megetron Posted April 25, 2012 Author Share Posted April 25, 2012 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.... Quote Link to comment https://forums.phpfreaks.com/topic/261577-how-do-i-wait-for-javascript-file-to-be-fully-loaded/#findComment-1340401 Share on other sites More sharing options...
haku Posted April 25, 2012 Share Posted April 25, 2012 What are you actually trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/261577-how-do-i-wait-for-javascript-file-to-be-fully-loaded/#findComment-1340416 Share on other sites More sharing options...
megetron Posted April 25, 2012 Author Share Posted April 25, 2012 What are you actually trying to do? I would like to provide a script for webmasters. the webmaster calls my php page (with javascript call) and gets html advertisment... Quote Link to comment https://forums.phpfreaks.com/topic/261577-how-do-i-wait-for-javascript-file-to-be-fully-loaded/#findComment-1340418 Share on other sites More sharing options...
rythemton Posted April 26, 2012 Share Posted April 26, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261577-how-do-i-wait-for-javascript-file-to-be-fully-loaded/#findComment-1340789 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.