8

Is there any event which is generated when the address bar text is modified by the user? Like, when user tries to execute javascript through address bar.

I have checked all the window events, none of them relate to address bar.

Update

As per the comments, I have tried window.onhashchange, but it only fires if the hash changes, not for the actual URL.

The reason I need this to find a way to stop the user from executing javascript from address bar. So, the timer solution won't work either.

Is there any way I can catch the onbeforeunload and set the location back to the previous one?

ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
  • 1
    http://stackoverflow.com/questions/1930927/how-can-i-detect-an-address-bar-change-with-javascript – Quannt Jan 07 '13 at 09:43
  • It is not a duplicate, the other question is for `hash` change and it won't work for me. – ATOzTOA Jan 07 '13 at 12:14

2 Answers2

2

There is no such event (native I mean)

Here is a subject about how to make an event out of window's location changes (uses setInterval):

http://www.bennadel.com/blog/1520-Binding-Events-To-Non-DOM-Objects-With-jQuery.htm

If you're using ajax, you might wanna check this out:

http://www.asual.com/jquery/address/

Armel Larcier
  • 15,747
  • 7
  • 68
  • 89
1

There is none such trick, but maybe the window.onbeforeunload can help you. With this you can know at least, when the user leaves your page.

See an example of it here: https://web.archive.org/web/20211028110528/http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm

Itchy
  • 2,263
  • 28
  • 41
  • Still, even if I catch the `onbeforeunload` event, I won't be able to stop the user, right? – ATOzTOA Jan 07 '13 at 10:02
  • You can not force him to, but you can ask him to. There will be a window displayed asking him, whether he really wants to leave. – Itchy Jan 07 '13 at 10:09
  • Can I redirect the page back to the original when `onbeforeunload` fires? – ATOzTOA Jan 07 '13 at 14:45
  • This depends on the browser that is used. In all newer ones: I think you can't. – Itchy Jan 07 '13 at 14:53