Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Do Something when User Forcing to Stop the Browser Loading

  • This topic is empty.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #41764
    Taufik Nurrohman
    Participant

    So, basically I want to load an external file with JavaScript on page load. But want to do the same thing when users force to stop the browser loading process of a page that hasn’t finished loading (for slow connections). Is this possible?

    **Illustration:**

    window.onload = doSomething;
    window.onstop = doSomething;
    #119893
    Andy Howells
    Participant

    What if you loaded the script by default but cancelled the function on load?

    Alternatively, if the JS is manipulating elements on the page, what if you used CSS classes? I.E a default ‘not-loaded’ class on your body element that the JS manipulates. Then have JS when loaded properly change the body class like ‘loaded’ and therefore that would respond to the JS in that circumstance?

    #119898
    Taufik Nurrohman
    Participant

    @andy_unleash I’m confusing :p I want to trigger the same thing in two situations: Loaded, or Forced to Stop.

    I don’t want the external files (script or stylesheet) loaded by default. So basically I want to load JSON (for the example) only when the page is loaded. But, if the user stop the page loading because of the slow connection or something, it would also trigger the JSON to loading:

    var loaded = 0;

    function loadJSON() {
    $.ajax({
    url: 'hxxp://blabla.com/feeds/posts/summary?alt=json-in-script',
    success: function() {
    alert('Hopla!');
    }
    });
    }

    $(window).on("what-custom-event??", function() {
    loadJSON();
    loaded = 1;
    });

    $(window).on("load", function() {
    if (loaded === 0) {
    loadJSON();
    }
    });

    The logic is: If the page has successfully loaded, a `Hopla!` message will appear. If the page loading is stopped by force, a `Hopla!` message will appear too.

    #119900
    Andy Howells
    Participant

    I may be wrong but why can’t you just not use onload? Can’t it just be a self executing function?

    #119902
    Taufik Nurrohman
    Participant

    The main priority is that the function will triggered after the page successfully loaded.

    A simple case: A loading animation layer that covered the whole page will appear before the page has been loaded, and will fade-out if the page has been loaded. But what if the page still does not loaded? If only I can make the layer invisible when users forcing to stop the browser loading, then that’s what I need.

    By the way, my main goal is to load the JSON on two situations that I wrote above, because this part is quite important, but too large to be loaded in the same time with others and it’s likely will interfere with other loading processes.
    It’s OK. I will also try to find the way in other places as well. If any…

    #119884
    JohnMotylJr
    Participant

    Can’t you just use a try catch block?

    #119961
    Taufik Nurrohman
    Participant

    @JohnMotylJr: How? I don’t know.

    PS: I got an example of JavaScript event called `onerror` and `onabort` but it’s not working for `window` (as far as I’v tried).

    #119962
    JohnMotylJr
    Participant
    try {
    //... Code you want to run...
    } catch (the error you want to catch) {
    //... Do this stuff instead...
    }
    #119965
    JohnMotylJr
    Participant

    Nevermind, I read your above and I think your logic is just a little off. Your not wanting to load externals if something is forced to stop loading?

    #119973
    Taufik Nurrohman
    Participant

    @JohnMotylJr: Contrarily. I want JavaScript to do something when the browser totally stop doing something.

    Onload means “stop doing something”.
    Then, what about Stopped?

    I just find it difficult to describe and to exist.

Viewing 10 posts - 1 through 10 (of 10 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.