How to display loading dialog when someone clicks a specific link?

前端 未结 3 869
春和景丽
春和景丽 2021-02-15 09:38

I do have an URL which opens a webpage which is very slow to load and I have no control over it.

I do want to display a loading dialog when someone clicks this URL or to

3条回答
  •  [愿得一人]
    2021-02-15 10:32

    Although ajax would be more elegant, it's possible. You have to intercept the navigation by preventing the default event, then force an update to the UI, then change the location to the destination url. Something like this:

    $('#mylink').click(function(e) {
        e.preventDefault();
        var url = this.href;
        // Update the UI here
        setTimeout(function() {
            // This is a trick to force a repaint
            window.location.href = url;
        },0);
    });
    

提交回复
热议问题