javascript - Detect if a new page is being loaded - Stack Overflow

Is there a way to detect via JavaScript, whether a new page is being loaded. Let me give you an example

Is there a way to detect via JavaScript, whether a new page is being loaded. Let me give you an example.

Another page is being loaded after I on this link:

<a href='/'>Click here</a>

There's no page load after I click here:

<a href='#anchor'>Click here</a>

No page load here either:

<a href='javascript:void(0);'>Click here</a>

Yep, that's a page load. But the href attribute is not URL of the page being loaded:

<a href='javascript:void(document.location="/");'>Click here</a>

This has URL in href, but it will not load a page. The onclick attribute is here just to illustrate the problem. In real world, the onclick event would probably by attached by event listener and canceled by event.preventDefault() somewhere else in the code:

<a href='/' onclick='return false;'>Click here</a>

Now imagine, that the next page takes long time to load. What I need is a way to detect whether a new page is being loaded, after I click on the link. Or after a form submit. Or after any action that can result in page load. I know which actions to watch, but I can't be sure if they necessarily trigger the page load.

Is it even possible? If so, how?

Thanks.

Is there a way to detect via JavaScript, whether a new page is being loaded. Let me give you an example.

Another page is being loaded after I on this link:

<a href='http://google./'>Click here</a>

There's no page load after I click here:

<a href='#anchor'>Click here</a>

No page load here either:

<a href='javascript:void(0);'>Click here</a>

Yep, that's a page load. But the href attribute is not URL of the page being loaded:

<a href='javascript:void(document.location="http://google./");'>Click here</a>

This has URL in href, but it will not load a page. The onclick attribute is here just to illustrate the problem. In real world, the onclick event would probably by attached by event listener and canceled by event.preventDefault() somewhere else in the code:

<a href='http://google./' onclick='return false;'>Click here</a>

Now imagine, that the next page takes long time to load. What I need is a way to detect whether a new page is being loaded, after I click on the link. Or after a form submit. Or after any action that can result in page load. I know which actions to watch, but I can't be sure if they necessarily trigger the page load.

Is it even possible? If so, how?

Thanks.

Share asked Mar 6, 2014 at 12:29 FczbkkFczbkk 1,4671 gold badge11 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The beforeunload event is probably what you are looking for. Such event handlers are usually used, for example, to prevent user from accidentally leaving a page with a filled form, by showing a confirmation dialog with a custom text message.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744914674a4600762.html

相关推荐

  • javascript - Detect if a new page is being loaded - Stack Overflow

    Is there a way to detect via JavaScript, whether a new page is being loaded. Let me give you an example

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信