Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually dispatch hashchange event

Tags:

javascript

1 - What is correct syntax for WebKit (primary for Safari on iPad, good if it works on Win Chrome) to .addEventListener to hashChange event?

2 - Is is possible (and how) to manually/programmatically dispatch hashChange event by .dispatchEvent on browsers mentioned above?

TIA.

I've found answer for 1:

window.addEventListener("hashchange", function() {console.log(location.hash)});

But I still have no idea how to dispatch hashchange manual since I don't know what EVENTOBJECT I should pass to window.dispatchEvent(EVENTOBJECT).

like image 913
grasnal Avatar asked Feb 22 '13 23:02

grasnal


1 Answers

If you want to force hashchcange event without literaly changing hash you should call:

window.dispatchEvent(new HashChangeEvent("hashchange"))

Object passed to the event handler will have this props available:

String oldURL;
String newURL;

This is an only info I found about this:

https://github.com/WebKit/webkit/blob/master/Source/WebCore/dom/HashChangeEvent.h

after receiving answer here:

http://forum.php.pl/index.php?showtopic=213470

like image 165
grasnal Avatar answered Oct 11 '22 11:10

grasnal