Link to home
Start Free TrialLog in
Avatar of travelgr
travelgr

asked on

How can I stop loading a Javascript?

How can I stop loading a Javascript, included with <script> tag eighter in head or in body section ? I have a JS from another domain what sometimes stop to works and just infinitely loading (I think till timeout occurs). I setted up my own timeout, what sets the src of the script to null ('')  after 10 seconds, but this solution doesn't work.
ASKER CERTIFIED SOLUTION
Avatar of Graham N.
Graham N.
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi,

the below code removes the script tag from the html dynamically, I dont know whether that stops the loading, just give a try.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>111111111111111</title>
<script type="text/javascript" src="somepath.js"></script>
</head>

<body>
<img src="hello" />
<script>
var id = -1;
scriptTags = document.getElementsByTagName('script');
for(i=0;i<scriptTags.length;i++)
{
//alert(scriptTags[i].src);
if(scriptTags[i].src=="somepath.js")
 id = i;
}
if(id!=-1)
{
      //alert('hi');
      parentTag = document.getElementsByTagName('head')[0];
      parentTag.removeChild(scriptTags[id]);
}      

</script>
</body>
</html>


replace somepath.js with the location of your js file.


kiranvj
@kiranvj: The browser will already have issued the "GET" instruction and will not execute any of the scripts in a page until all the scripts have either downloaded or timed out. In IE you have the added issue that that it waits for all the HTML as well before executing scripts. Nice idea of your though.
thanks for the info grahamnonweiler

another idea is can we load the js file using ajax or some other method and check the time taken to load. If a specified time is elapsed stop loading?? what do u think
Using AJAX - again nice idea - worth a try! However it will depened on what the JS is supposed to do - it could include AJAX calls itself which would really make it interesting!
yes, we can give a try. travelgr will be able to give more idea about the js file.
@travelgr: Can you tell us what the JS does - or at least some idea so that an answer can be provided to you.
How about

var exScript = document.createElement('script');
exScript.src='http://www.bla.com/somescript.js';
var headTag = document.getElementsByTagName('head')[0];
headTag.appendChild(exScript);

It is actually the opposite of kiranvj's script
Avatar of travelgr
travelgr

ASKER

Hi guys!

Some details about my problem: I have a Zedo Ad script on the header of my page.
Together with some JS variables and an Iframe this should show up a travel ad, but sometimes eighter the script or the iframe blocks my page (I think the script is responsable for that). Btw in my opinion the script does some tracking issues, so I have to link it every single time.
Previously the script and the iframe was on the header section, and when stopped to work stopped the whole page to load. Now there is an event attached to the DOM: when it is ready then start the script and iframe loading. Now if the script stops, my page is already loaded and works, just a little bit slower because of the script.

I'm using PHP5 on Apache Web Server.
grahamnonweiler: >> A more complex option (if you server-side scripting access) is to pull the javascript in to your server-side script and then place it in your html, that way you could determine any timeout you wish.
How can I do this from PHP? I know file_get_contents instruction.

About the ajax solution: I don't think that the JS file is making Ajax Requests. But how can I set a timeout?