473,467 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3174
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: hemant.singh | last post by:
Hello all, I am try'g to send window.location.href to the server script who will generate dynamic javascript according to the referral name comg in as param Now bcz <script language="javascript"...
12
by: Iddo | last post by:
Hi, I am having a strange problem... I have an HTML file which has 2 script tags: 1) <script language="javascript" id="ABC" src="ABC.js" /> 2) <script id="general" language="javascript">...
44
by: rhythmace | last post by:
W3C HTML validator passes this: .... <script type="text/javascript" src="foo.js"> <script type="text/javascript"> ....script in here... </script> ....
2
by: -Karl | last post by:
Couls someone please advise me on this error. What I am trying to do is be able to convert an XML document into arrays. I read that the subs & functions have to be in <scripttags. Thanks! ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.