0

I want to add an alert("some text") message after document.location.href or some kind of related code.

ex:

document.location.href = "http://www.msn.com";
windows.alert("You are now at msn.com");

By the moment I use document.location.href I automatically switch site so the rest of the code isn't being done. So I would like to know if there is an other way to switch site on the same page/ not on the same page and after that put my custom code and it will be executed accordingly?

  • 2
    you can only execute YOUR code on YOUR page. can you go to someone else's page and use your code? no. – PlantTheIdea Mar 28 '13 at 22:14
  • Unless you have control over the new page, you can't do this. I think the closest you can come is to open the new page in a different window and then do your alert in your page's window. – atw13 Mar 28 '13 at 22:14
  • 1
    Imagine someone doing this, but instead of `msn.com`, they redirect you to your bank's website and change the login form so that it sends your password to a remote server. Browsers don't allow this to happen. – Blender Mar 28 '13 at 22:14
  • 2
    [`window.onbeforeunload`](https://developer.mozilla.org/en-US/docs/DOM/window.onbeforeunload)? – Teemu Mar 28 '13 at 22:27

1 Answers1

1

Use the haschange event:

$(window).on('hashchange', function() {
  // your code here
});

See this post

What have you tried
  • 11,018
  • 4
  • 31
  • 45