我如何让一个JavaScript动作对当前页面有一些影响,但也会改变浏览器中的URL,这样当用户点击重新加载或书签时,就会使用新的URL ?
如果后退按钮可以重新加载原始URL,那就太好了。
我试图在URL中记录JavaScript状态。
我如何让一个JavaScript动作对当前页面有一些影响,但也会改变浏览器中的URL,这样当用户点击重新加载或书签时,就会使用新的URL ?
如果后退按钮可以重新加载原始URL,那就太好了。
我试图在URL中记录JavaScript状态。
当前回答
我的代码是:
//change address bar
function setLocation(curLoc){
try {
history.pushState(null, null, curLoc);
return false;
} catch(e) {}
location.hash = '#' + curLoc;
}
和行动:
setLocation('http://example.com/your-url-here');
和示例
$(document).ready(function(){
$('nav li a').on('click', function(){
if($(this).hasClass('active')) {
} else {
setLocation($(this).attr('href'));
}
return false;
});
});
这就是全部:)
其他回答
jQuery有一个很棒的插件,可以改变浏览器的URL,叫做jQuery-pusher。
JavaScript pushState和jQuery可以一起使用,比如:
历史。pushState(null, null, $(this).attr('href'));
例子:
$('a').click(function (event) {
// Prevent default click action
event.preventDefault();
// Detect if pushState is available
if(history.pushState) {
history.pushState(null, null, $(this).attr('href'));
}
return false;
});
只使用JavaScript history.pushState(),它会更改引用器,在更改状态后创建的XMLHttpRequest对象的HTTP报头中使用它。
例子:
window.history。pushState("object", "Your New Title", "/ New -url");
pushState()方法:
pushState()接受三个参数:一个状态对象、一个标题(当前被忽略)和一个URL(可选)。让我们更详细地分析这三个参数:
state object — The state object is a JavaScript object which is associated with the new history entry created by 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. 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 her browser, we impose a size limit of 640k characters on the serialized representation of a state object. If you pass a state object whose serialized representation is larger than this to pushState(), the method will throw an exception. If you need more space than this, you're encouraged to use sessionStorage and/or localStorage. title — Firefox currently ignores this parameter, although it may use it in the future. Passing the empty string here should be safe against future changes to the method. Alternatively, you could pass a short title for the state to which you're moving. 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 her 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.
我给出了一个更简单的答案,
window.history.pushState(null, null, "/abc")
这将在浏览器URL的域名后面添加/abc。只需复制这段代码并将其粘贴到浏览器控制台,就可以看到URL更改为“https://stackoverflow.com/abc”
什么是为我工作是- history.replaceState()函数这是如下-
history.replaceState(data,"Title of page"[,'url-of-the-page']);
这将不会重新加载页面,你可以使用javascript的事件
我的代码是:
//change address bar
function setLocation(curLoc){
try {
history.pushState(null, null, curLoc);
return false;
} catch(e) {}
location.hash = '#' + curLoc;
}
和行动:
setLocation('http://example.com/your-url-here');
和示例
$(document).ready(function(){
$('nav li a').on('click', function(){
if($(this).hasClass('active')) {
} else {
setLocation($(this).attr('href'));
}
return false;
});
});
这就是全部:)
有一个jquery插件http://www.asual.com/jquery/address/
我觉得这就是你需要的。