Javascript/jQuery: programmatically follow a link

前端 未结 2 887
南方客
南方客 2021-01-03 22:59

In Javascript code, I would like to programmatically cause the browser to follow a link that\'s on my page. Simple case:



        
2条回答
  •  时光说笑
    2021-01-03 23:43

    As far as I know, window.location does exactly what you are looking for, triggering the browser's default link clicking behavior.

    Some browsers notice the protocol before any events are fired or the actual href is changed.

    window.location = "mailto:somebody@example.com";
    

    Trying the fiddle demo mentioned below I get the following results:

    • Chromium: fires onbeforeunload with the button and link
    • Firefox fires onbeforeunload only for the button
    • Safari: never fires onbeforeunload
    • Opera: same as Safari

    So a good way to prevent the unload event from being fired is by returning false in beforeunload.

提交回复
热议问题