没有页面加载的window.location

时间:2013-09-29 09:29:32

标签: javascript jquery hash

我目前在hashchange和jQuery移动冲突方面遇到了一些问题,因此我希望手动更改网址并自行处理请求。
虽然有轻微的问题,我现在正在使用:

$('#test').click(function() {
    window.location = "/test/#hash"; 
});

哪个成功更改了我想要的网址,但是然后尝试加载请求的网址,我只是想更改网址而不是提示页面加载。这是可能吗?任何建议将不胜感激!

1 个答案:

答案 0 :(得分:1)

您可以使用pushState方法:

  

window.history.pushState({foo:'bar'},'other page','/ test / #hash');

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

请注意,< = IE9

不支持此功能

JAVASCRIPT

$('a').on('click', function(e) {
  var element = $(this),
      element_anchor = element.attr('href'),
      element_title = element.attr('title');
  if (typeof window.history !== 'undefined')
  {
    window.history.pushState({anchor: element_anchor}, element_title, '/test/' + element_anchor);
  }
  e.preventDefault();
});

HTML

<a href="#first_page" title="this is the first page">first page</a>
相关问题