B
boczek
Hello all.
I'm writing a small async webapp. in JavaScript and I'm using <script>
element technique to load data.
I'm usign <script> because 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()*Math.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?
I'm writing a small async webapp. in JavaScript and I'm using <script>
element technique to load data.
I'm usign <script> because 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()*Math.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?