I have 2 buttons, A and B. On clicking A the hashtag changes and I want to call the window.onhashchange
function. But, on clicking B, I am manually appending a hashtag to the URL. In the later case, I don't want window.onhashchange
to be triggered.
Asked
Active
Viewed 326 times
0

Jason Aller
- 3,541
- 28
- 38
- 38

spyder
- 330
- 3
- 6
- 18
-
Can't you ignore the hashtag in function? Or remove hashchange handler before changing and append it back on done? – Vyktor Apr 15 '13 at 15:19
-
@Vyktor How can i remove hashchange handler before changing and append it back on done? – spyder Apr 15 '13 at 15:21
-
@Fred i hav tried a lotta things like using a counter. my page is loaded once and then i am manipulating it rest of the time. actually i want to capture the back and forward button click. and i am manually updating the hashtags. i dont want window.onhashchange to interfere in these occassions... – spyder Apr 15 '13 at 15:24
3 Answers
0
Without any code all I can suggest is to check the id of the button that causes the event and act accordingly.

Useless Intern
- 1,294
- 1
- 10
- 20
0
With little help of this answer and this one you can temporary remove event handles. Here's working example of jsfiddle.net with click
event so assuming jQuery >= 1.8
it'll be similar, thus something like this:
// Initialization, add some events
$.hashchange(...);
// Store event handles for hashchange
hashchange_handles = [];
$.each( $._data($('#foo').get(0), "events")['hashchange'], function(i,handle){
// alert(handle.handler);
hashchange_handles.push(handle.handler);
});
// Remove handles
$.off('hashchange');
// Do your stuff here
// Restore event handler
length = hashchange_handles.length;
for(var i=0; i<length; i++) {
$.hashchange( hashchange_handles[i]);
}
It worked for click
so hopefully it'll work for hashchange
.
Note: I know the code is not perfect, you should tuned it. Plus I have tested it only for click
.
-
hey thanks @Vyktor...i'll try this... btw is there any way to detect the source of hashchange? – spyder Apr 15 '13 at 18:21
-
@spyder I doubt so, but you won't loose anything by googling for a while :) – Vyktor Apr 15 '13 at 18:48
0
From your question I'm assuming you have generated HTML by PHP something along this:
<ul id='list-of-links'>
<li id='A'><a href="#posA">Link A</a></li>
<li id='B'><a href="#posB">Link B</a></li>
</ul>
Having that you wish that A does something else than B. In your JavaScript you can use the hash function.
var targetHash = window.location.hash.slice(1);
if( targetHash != 'posB' ) {
//do something as it's not 'posB'
};
Hope that helps.

rkeet
- 3,406
- 2
- 23
- 49
-
-1, Based on [javascipt operator precedence](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Operator_Precedence) is `!targetHash` evaluated before `==`, therefore you are doing `False == 'posB'` and why not to use `!=` directly? – Vyktor Apr 15 '13 at 18:53
-
Yea was my mistake, too much of a quick answer without proper testing, thanks for removing downvote. – rkeet Apr 15 '13 at 19:05
-
thanks a lot guys! i found the answer to this.. https://developers.google.com/tv/web/articles/location-hash-navigation#detect-loc-hash can help!!! – spyder Apr 16 '13 at 01:43