最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run code after changing the URL via window.location? - Stack Overflow

programmeradmin3浏览0评论

I'm doing this but it doesn't work:

window.addEventListener("load", function load(event){
    alert('hola');
},false);

window.location.assign("about:blank");

It's a Greasemonkey script. The new location is loaded but the alert is never shown.

I'm doing this but it doesn't work:

window.addEventListener("load", function load(event){
    alert('hola');
},false);

window.location.assign("about:blank");

It's a Greasemonkey script. The new location is loaded but the alert is never shown.

Share Improve this question edited Mar 8, 2013 at 7:55 Brock Adams 93.7k23 gold badges241 silver badges305 bronze badges asked Mar 7, 2013 at 16:11 gal007gal007 7,2129 gold badges50 silver badges73 bronze badges 3
  • Of course the alert never occurs. Once the location has changed, the user is no longer on your site... – BenM Commented Mar 7, 2013 at 16:15
  • 1 As this is a Greasemonkey script, why not just enable it (or create a different one) that works on the new location URLs? – Paul Butcher Commented Mar 7, 2013 at 16:58
  • possible duplicate of Javascript: Detect when a window is fully loaded – Sindre Sorhus Commented Jun 14, 2013 at 10:25
Add a ment  | 

2 Answers 2

Reset to default 5

Once you change the window.location, the current instance of your Greasemonkey script is purged. To "run code" after the location change, you need to set the script to trigger on the new page (about:blank in this case), and then use a flag to signal that the new page was reached via this script redirecting the original page.

  1. Make sure that the script's @include or @match directives fire on the new page.
  2. Use GM_setValue() to set the flag letting the script know it has been deliberately reincarnated.

Here is a plete, working script that illustrates the process:

// ==UserScript==
// @name     _Fire after redirect to about:blank
// @include  about:blank
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis./ajax/libs/jquery/1.7.2/jquery.min.js
// @grant    GM_setValue
// @grant    GM_getValue
// @grant    GM_deleteValue
// ==/UserScript==

//-- Are we on a blank page after a redirect by this script?
var bAfterRedirect = GM_getValue ("YouHaveBeenRedirected", false);

//-- Always erase the stored flag.
GM_deleteValue ("YouHaveBeenRedirected");

if (bAfterRedirect  &&  location == 'about:blank') {
    //-- DO WHATEVER YOU WANT WITH THE BLANK/NEW PAGE HERE.
    $("body").append (
        '<h1>This content was added after a GM redirect.</h1>'
    );
}
else if (location != 'about:blank') {
    /*-- If we are on the original target page, signal our next incarnation
        that it was triggered by a redirect.  Then redirect to about:blank.
    */
    GM_setValue ("YouHaveBeenRedirected", true);
    location.assign ("about:blank");
}

By only changing the hash (or fragment) part of the url, then doing whatever it is you want to do, thus:

window.location.assign("#hello")
alert("hola")

or thus:

window.location.hash = "world"
alert("mondo")

If the document has already loaded, onload will not trigger again, so you cannot run it from a load event. HTML5, offers a hashchange event, which you could use, thus:

window.addEventListener("hashchange", function (event){
    alert('change');
},false);

Some javascript libraries (I know dojo does) implement hashchange or an equivalent for non-HTML5 browsers. In order to take advantage of that, you would need to use that library's convention for registering for events.

发布评论

评论列表(0)

  1. 暂无评论