Created
July 24, 2010 21:40
-
-
Save benjamn/488983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
function action() { | |
// Whatever you want to do. | |
alert("came back"); | |
} | |
// In case the page gets reloaded upon coming back. | |
if (/#redirected$/.test(location.href)) { | |
action(); | |
return; | |
} | |
window.onload = function() { | |
var a = document.body.appendChild(document.createElement("a")); | |
a.href = "http://example.com"; // Wherever you want to go. | |
location = location.href + "#redirected"; | |
// Needs to fire just after the window has finished loading, or the | |
// original page will not remain in history. | |
setTimeout(function() { | |
try { | |
// This works in webkit. | |
var e = document.createEvent("MouseEvent"); | |
e.initMouseEvent("click", true, true); | |
a.dispatchEvent(e); | |
} catch (x) { | |
// This works in Firefox. | |
location.assign(a.href); | |
} | |
}, 10); | |
}; | |
// We rely on this interval not firing before the window unloads. | |
window.onunload = function() { | |
var interval = setInterval(function() { | |
if (/#redirected$/.test(location.href)) { | |
clearInterval(interval); | |
action(); | |
} | |
}, 10); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment