How to abort loading of dynamically included <script>

Discussion in 'Javascript' started by boczek, Jan 4, 2007.

  1. boczek

    boczek Guest

    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

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

    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 = null;

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

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

    CURRENT = element;
    document.body.innerHTML += 'Loading: '+ id +'<br />';

    function makeSomeNoise() {

    if (COUNTER < 10) {
    setTimeout(makeSomeNoise, 200);


    And some.php code:

    echo 'document.body.innerHTML += "Loaded: '. $_GET['id'] .'<br />";';

    Anyone can help?
    boczek, Jan 4, 2007
    1. Advertisements

  2. 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 or probably better
    first a request in 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, Jan 4, 2007
    1. Advertisements

  3. boczek

    boczek Guest

    Martin Honnen napisal(a):
    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.
    boczek, Jan 4, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.