admin管理员组文章数量:1426648
When I do this,
location.hash = "test"
the url is updated and the page homes in on the element with that id.
Is there a way to stop the page from homing in to that element?
When I do this,
location.hash = "test"
the url is updated and the page homes in on the element with that id.
Is there a way to stop the page from homing in to that element?
Share Improve this question edited Nov 11, 2012 at 4:47 Zirak 39.9k13 gold badges85 silver badges92 bronze badges asked Nov 11, 2012 at 4:43 developarvindeveloparvin 5,05912 gold badges57 silver badges101 bronze badges 9-
2
location.hash
has nothing to do with jQuery, it is part of JavaScript. jQuery is JavaScript framework. – Tadeck Commented Nov 11, 2012 at 4:45 -
Why do you change
location.hash
if you don't want that effect? – Zirak Commented Nov 11, 2012 at 4:45 - @Tadeck I am working under that framework. It really doesn't matter to me if the solution is in pure JS or jQuery as long as it works. – developarvin Commented Nov 11, 2012 at 4:47
- @Zirak It is used in tab history. It is working fine. I just don't want to home in on the element for aesthetic reasons. – developarvin Commented Nov 11, 2012 at 4:49
- Check out the history api – Zirak Commented Nov 11, 2012 at 4:51
2 Answers
Reset to default 3Solution
You cannot prevent this behaviour, but you can fool it by temporarily hiding the target, eg. like that (it has nothing to do with jQuery):
// obtain "target" DOM (not jQuery) element and do this:
var original_id = target.id; // save original ID
target.id = null; // set target's ID
location.hash = 'test'; // issue location.hash change
target.id = original_id; // set ID to original value
Generalized solution
Or more general example:
// make target_hash set to a hash you want to not lead you to page section and:
var element = document.getElementById(target_hash); // find element by ID
var original_id = element.id; // save original ID
location.hash = target_hash; // previously defined by you (eg. 'test')
element.id = original_id; // reset ID
Demo / proof
The live example can be as follows, in the event handler attached through jQuery (demo here: http://jsfiddle/DaZfH/):
some_link.on('click', function(event){
event.preventDefault();
var target = document.getElementById('target');
var original_id = target.id;
target.id = null; // unset ID
location.hash = 'target';
target.id = original_id;
});
Disclaimer
But indeed others are right: moving you to the correct place in the document is the correct behaviour. If you are doing things like I mentioned, then your solution is pretty hakish and there is definitely a better way to do that.
Is there a way to stop the page from homing in to that element
Yes. Although the hashchange event is not cancelable, you can reset its unwanted behavior like this.
var popY,
popX;
//When location.hash is changed, popstate is the first event to fire
//Use this event to record current state
window.addEventListener('popstate', popstateHandler);
function popstateHandler() {
popY = window.scrollY;
popX = window.scrollX;
}
//Reset scroll position with recorded values when hashchange fires (after page is scrolled)
window.addEventListener('hashchange', hashchangeHandler);
function hashchangeHandler() {
window.scrollTo(popX, popY);
}
That's the basis of it. You might want to do some proofing for IE, and implement your reasons for doing this: Animate scroll, active something etc..
Polyfill for scrollY, scrollX:
if(!('scrollY' in window)) {
Object.defineProperty(window, 'scrollY', {
get: function () {
return window.pageYOffset
}
});
Object.defineProperty(window, 'scrollX', {
get: function () {
return window.pageXOffset
}
})
}
本文标签: javascriptPrevent default behavior when setting locationhashStack Overflow
版权声明:本文标题:javascript - Prevent default behavior when setting location.hash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745471883a2659786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论