简体   繁体   English

更改URL,无需重新加载页面

[英]Change the url without need to reload the page

I try to change the url of a page without need to reload the page. 我尝试更改页面的URL,而无需重新加载页面。 If I try this in console the URL is changing: 如果我在控制台中尝试此操作,URL将会更改:

if (localStorage.getItem('product') !== null) {
    storedVariable = localStorage.getItem('product');
    url = window.location.href;
    url += '?product=' + storedVariable;
}
window.history.pushState("", "", url);

However when I put this code to google tag manager the URL of my page is the default. 但是,当我将此代码放到Google跟踪代码管理器中时,页面的URL是默认的。 How can I fix it? 我该如何解决?

Maybe related to : mdn History_API 可能与: mdn History_API有关

pushState() takes three parameters: a state object, a title (which is currently ignored), and (optionally) a URL. pushState()具有三个参数:状态对象,标题(当前被忽略)和(可选)URL。 Let's examine each of these three parameters in more detail: 让我们更详细地检查这三个参数:

  • state object — The state object is a JavaScript object which is associated with the new history entry created by pushState(). 状态对象—状态对象是一个JavaScript对象,它与pushState()创建的新历史记录条目相关联。 Whenever the user navigates to the new state, a popstate event is fired, and the state property of the event contains a copy of the history entry's state object. 每当用户导航到新状态时,都会触发popstate事件,并且该事件的state属性包含历史记录条目的状态对象的副本。

    The state object can be anything that can be serialized. 状态对象可以是任何可以序列化的对象。 Because Firefox saves state objects to the user's disk so they can be restored after the user restarts the browser, we impose a size limit of 640k characters on the serialized representation of a state object. 因为Firefox将状态对象保存到用户的磁盘上,以便用户重新启动浏览器后可以将其还原,所以我们对状态对象的序列化表示施加了640k个字符的大小限制。 If you pass a state object whose serialized representation is larger than this to pushState(), the method will throw an exception. 如果将序列化表示形式大于此形式的状态对象传递给pushState(),则该方法将引发异常。 If you need more space than this, you're encouraged to use sessionStorage and/or localStorage. 如果您需要更多空间,建议您使用sessionStorage和/或localStorage。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM