window.location.reload在所有版本的IE中重新加载时都不保存#hash值

时间:2014-03-05 19:26:18

标签: javascript jquery internet-explorer reload

我正在尝试使用#hash值重新加载页面,以便在页面刷新时获取哈希值并吐出一个函数。这适用于除IE的所有版本之外的所有浏览器。当我点击链接时,它会添加#value,但在重新加载后会显示#undefined。

知道可能导致这种情况的原因吗?

注意:

  1. 我需要重新加载的页面
  2. 它不需要去哈希的位置,它需要从顶部查看整个页面
  3. //refreshes the page when the #by-date-link is clicked $('.sort-trigger').on('click', function(e){ e.preventDefault(); var urlHash = $(this).attr('href'); window.location.hash = urlHash; window.location.reload(); });

1 个答案:

答案 0 :(得分:0)

为什么重装?另外,我假设href attr是#something - 这是一个哈希。转储location.hash并使用onhashchange是不是更有意义?

编辑:没有正确阅读。我的猜测是,preventDefault与单击锚定链接相结合会给IE带来问题,所以可能会将hash存储在localstorage(或类似的东西)并将其设置为onload?

还发现了这个:

if(event.preventDefault){
    event.preventDefault();
  } else {
    event.returnValue = false;
}

on http://blog.calebnance.com/javascript/how-to-stop-event-actions-from-firing-on-an-element-with-javascript.html

并且:event.preventDefault() function not working in IE