469,572 Members | 1,202 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

How to abort loading of dynamically included <script>

Hello all.

I'm writing a small async webapp. in JavaScript and I'm using <script>
element technique to load data.
I'm usign <scriptbecause of cross domain restrictions with
XmlRequest.

The problem is when I remove a element that is currently loading from
DOM (with removeChild) Firefox still loads it and waits with other
scripts.

What I want is to break this process and load a new data without
waiting for old one.

Here is example script:

<script type="text/javascript">
var COUNTER = 0;
var CURRENT = null;
function loadScript(id) {
//abort previous script
if (CURRENT !== null) {
document.body.innerHTML += 'Abort: '+ id + '<br />';
CURRENT.parentNode.removeChild(CURRENT);
CURRENT = null;
}

//create new script
var element = document.createElement('script');

//load script with new params
element.id = id;
element.src = 'some.php?id='+id;
element.type = 'text/javascript';
element.charset = 'utf-8';

CURRENT = element;
document.body.innerHTML += 'Loading: '+ id +'<br />';
document.getElementsByTagName('head')[0].appendChild(element);
}

function makeSomeNoise() {
COUNTER++;

loadScript('script_'+COUNTER+'_'+(Math.random()*Ma th.random()).toString().substr(2));
if (COUNTER < 10) {
setTimeout(makeSomeNoise, 200);
}
}

makeSomeNoise();
</script>
And some.php code:
<?php
sleep(3);
echo 'document.body.innerHTML += "Loaded: '. $_GET['id'] .'<br />";';
?>

Anyone can help?

Jan 4 '07 #1
2 2970
boczek wrote:
I'm writing a small async webapp. in JavaScript and I'm using <script>
element technique to load data.
I'm usign <scriptbecause of cross domain restrictions with
XmlRequest.

The problem is when I remove a element that is currently loading from
DOM (with removeChild) Firefox still loads it and waits with other
scripts.
I am not sure there is anything you can do about that. I don't think any
browser unloads script code when the HTML script element is removed from
the document. And I don't think you can force the browser to abort
loading of a resource when you remove an element referencing the
resource. With the "new demands" dynamic web applications have it might
be that a bug report on https://bugzilla.mozilla.org/ or probably better
first a request in mozilla.dev.tech.dom changes the behaviour in future
Mozilla versions.

Have you tested with other browsers? Which browsers abort the script
loading when you remove the script element from the document?
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 4 '07 #2

Martin Honnen napisal(a):
boczek wrote:
[...]
The problem is when I remove a element that is currently loading from
DOM (with removeChild) Firefox still loads it and waits with other
scripts.
[...]
Have you tested with other browsers? Which browsers abort the script
loading when you remove the script element from the document?
IE7 seems to load everything but not executing removed scripts.
Opera loads and executes all scripts.

Yes. It seems that it is not specified what to do when user removes
"script" element of currently loading code. I'll try to find other
solution.. maybe with splitting one connection into chunks and when new
connection is established I can stop sending remaining chunks.

Thx. for your post.

Jan 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.