Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get an event when the address bar is modified?

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?

like image 888
ATOzTOA Avatar asked Jan 07 '13 09:01

ATOzTOA


People also ask

How do I know if my URL has been changed?

You can use the popstate method to detect those URL changes and make UI changes as needed. window. addEventListener('popstate', function (event) { // The URL changed... });

What is the address bar use for?

The address bar is the familiar text field at the top of a web browser's graphical user interface (GUI) that displays the name or the URL (uniform resource locator) of the current web page. Users request websites and pages by typing either the name or the URL into the address bar.

What is window event in JavaScript?

JavaScript Window Events are associated with the windows object defined for describing the events. There are other ways also which can handle the windows event like using and defining within the body tag for the events but that makes the entire code and the event a bit cumbersome to understand and evaluate.


2 Answers

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/

like image 198
Armel Larcier Avatar answered Nov 15 '22 00:11

Armel Larcier


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: http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm

like image 45
Itchy Avatar answered Nov 15 '22 00:11

Itchy