1

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.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
gal007
  • 6,911
  • 8
  • 47
  • 70
  • Of course the alert never occurs. Once the location has changed, the user is no longer on your site... – BenM Mar 07 '13 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 Mar 07 '13 at 16:58
  • possible duplicate of [Javascript: Detect when a window is fully loaded](http://stackoverflow.com/questions/6424064/javascript-detect-when-a-window-is-fully-loaded) – Sindre Sorhus Jun 14 '13 at 10:25

2 Answers2

4

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 complete, 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.com/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");
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • It doesnt work. When location is loaded the document.onLoad is not called – gal007 Mar 08 '13 at 16:15
  • 1
    No, it works perfectly. (1) `document.onLoad` is not a valid function, (2) My answer uses no onload function. **Did you try my script? It works** (you just need to change the one include, (3) no onload is required for this issue ("run code after changing the URL"), (4) the window load event does fire, but it is not needed. – Brock Adams Mar 08 '13 at 23:12
1

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.

Paul Butcher
  • 6,902
  • 28
  • 39
  • window.addEventListener("hashchange", function load(event){ alert('hola'); },false); window.location.replace("http://www.yahoo.com"); – gal007 Mar 07 '13 at 16:41
  • And I need the alert be shown when the new document is loaded – gal007 Mar 07 '13 at 16:42
  • Of course it won't be in the example you give. Like I said in the first sentence, you can only change the hash (fragment) part. `window.location.replace("yahoo.com")` replaces the whole URL. You can't run code from an old page after changing the whole URL. – Paul Butcher Mar 07 '13 at 16:45
  • If, in the same script, you need to redirect a window to an entirely new page, then do something to that page, then no, it can't be done. – Paul Butcher Mar 07 '13 at 16:59
  • My real problem is that I need to do something with the dom document (I need to add some extra divs and another html elements). If the current url exists, there is no problem what realli is (www.google.com or www.yahoo.com are the same for my porpouse). But if the Browser is in about:home there is no dom document to add things. – gal007 Mar 07 '13 at 17:30
  • So, my idea is to redirect to about:blank and then add my extras. But I can't – gal007 Mar 07 '13 at 17:30
  • Working for me (part ... window.addEventListener("hashchange" ...). +1 – Severe Torture Dec 05 '17 at 07:53