window.history.pushState刷新浏览器 [英] window.history.pushState refreshing the browser

查看:1358
本文介绍了window.history.pushState刷新浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一些JavaScript code,并使用 window.History.pushState 加载新的 HTML 页面,而不是使用的href 标记。我的code(即做工精细)看起来是这样的。

  window.History.pushState({urlPath:'/第1页},,/第1页)

奇怪的是,这个失败,即重新加载浏览器

  window.History.pushState({urlPath:'/ page2.php中将},,/ page2.php中将')

但这个工作,内容更新,浏览器不会刷新! (注意URL是绝对的,不是相对)

<$p$p><$c$c>window.History.pushState({urlPath:'www.domain.com/page2.php'},\"\",'www.domain.com/page2.php')

的<一个href=\"https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history\">documentation为 window.History.pushState 表示,第三个参数的URL可以是绝对或相对 -


  

网址 - 新历史条目的URL通过这个参数给出。注意
  该浏览器将不会试图在通话结束后加载这个URL
  pushState的(),但它可能尝试以后加载的URL,比如
  用户后重新启动浏览器。新的URL并不需要是
  绝对;如果它是相对的,它的相对于当前的URL解决。
  新的URL必须是相同的原点为当前URL;除此以外,
  pushState的()将抛出一个异常。此参数是可选的;如果
  未指定,它被设置为文档的当前URL。


绝对URL似乎是工作,但相对似乎并非如此。为什么会出现这种情况?


解决方案

简短的回答是, history.pushState (不是 History.pushState ,这将抛出一个异常,窗口部分是可选的),我决不做你的建议是什么。

如果网页都令人耳目一新,那么它是由你在做其他的事情(例如,您可能有code运行了去,在地址栏中改变的情况下,一个新的位置)而引起的。

history.pushState({urlPath:'/ page2.php中将},,/ page2.php中将')工作完全像它在应该我和我的同事们的Chrome,IE和Firefox的最新版本。

其实你可以把任何你喜欢到函数: history.pushState({},'','这么久,感谢所有的fish.not一个真正的文件')

如果您发布一些code(特别注意为code附近 history.pushState ,任何地点 document.location 使用),那么我们将非常乐意帮助你找出究竟哪里这个问题的来源。

如果您发布更多code,我会更新这个答案(我还你的问题:收藏最多)。)

I am working on some javascript code, and using window.History.pushState to load new HTML pages, instead of using href tags. My code (which is working fine) looks like this.

window.History.pushState({urlPath:'/page1'},"",'/page1')

strangely, this fails, ie reloads the browser

window.History.pushState({urlPath:'/page2.php'},"",'/page2.php')

But this works, content is updated, browser not refreshed ! (notice the URL is absolute and not relative)

window.History.pushState({urlPath:'www.domain.com/page2.php'},"",'www.domain.com/page2.php')

The documentation for window.History.pushState says that the third parameter URL can be either absolute or relative -

URL — The new history entry's URL is given by this parameter. Note that the browser won't attempt to load this URL after a call to pushState(), but it might attempt to load the URL later, for instance after the user restarts the browser. The new URL does not need to be absolute; if it's relative, it's resolved relative to the current URL. The new URL must be of the same origin as the current URL; otherwise, pushState() will throw an exception. This parameter is optional; if it isn't specified, it's set to the document's current URL.

Absolute URLs seem to be working but relative seem to be not. Why is this happening?

解决方案

The short answer is that history.pushState (not History.pushState, which would throw an exception, the window part is optional) will never do what you suggest.

If pages are refreshing, then it is caused by other things that you are doing (for example, you might have code running that goes to a new location in the case of the address bar changing).

history.pushState({urlPath:'/page2.php'},"",'/page2.php') works exactly like it is supposed to in the latest versions of Chrome, IE and Firefox for me and my colleagues.

In fact you can put whatever you like into the function: history.pushState({}, '', 'So long and thanks for all the fish.not a real file').

If you post some more code (with special attention for code nearby the history.pushState and anywhere document.location is used), then we'll be more than happy to help you figure out where exactly this issue is coming from.

If you post more code, I'll update this answer (I have your question favourited) :).

这篇关于window.history.pushState刷新浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆