使用history.pushState()时更新整个location.pathName
Update entire location.pathName when using history.pushState()
我正在创建一个网站,将我的所有内容加载到我的主index.php中。我正在努力让它使用干净的URLS。
示例:http://www.host.com/home
这个URL会将home.php加载到index.php中。我正在使用history.pushstate()更新我的历史记录,但在某些页面上,我希望有一个更深入的URL。
示例:http://www.host.com/forum/topicName
这将把论坛页面加载到索引中,并从mySQL中获取主题名称并显示它。但从这里开始,如果我单击导航按钮,URL将显示为这样。。。
示例:http://www.host.com/forum/home
我想让它清除那个/论坛,然后离开/主页。到目前为止,这里是我的一些代码。
function myNav(whereTo)
{
window.history.pushState(null, null, whereTo);
var toLoad = 'pages/'+whereTo+'.php';
$('#mainContent').load(toLoad);
}
这就是我现在使用的快速解决方案,我希望有更好的方法。
if(window.location.pathname.lastIndexOf('/') != 0)
window.location.pathname = whereTo;
else
window.history.pushState(null, null, whereTo);
谢谢你。我想在中的URL开头添加一个/
window.history.replaceState(stateObj, title, URL);
将整个pathName替换为opposedin,与我的做法相反,只是添加页面名称。
相关文章:
- 正在使用$location.path(.)路由ng视图
- Reg-ex,用于解析window.location.pathname并返回个/s
- addClass('active') with window.location.pathname - u
- Javascript - 获取 location.pathname + location.search - 不占用所有部
- window.location.pathname Chrome vs Mozilla
- parent.window.location.pathname 返回未定义
- windows.location.pathname不起作用
- 使用history.pushState()时更新整个location.pathName
- 将location.pathname和location.search一起设置
- 如何“;转换“;window.location.pathname从对象到字符串
- 哪个更快:document.location.pathname或myOwnObj.pathname
- 在location.pathname前添加哈希值
- what return 'windows.location.pathname'?
- 如何使用位置.Href代替location.pathname
- 如何检查数组中的字符串是否在URL/window.location.pathname中
- 如何从window.location.pathname中删除尾部斜杠
- 如何防止window.location.pathname在URL中转义字符
- 如何从window.location.pathname获取文件名
- 相当于NodeJS中的window.location和window.pathname
- # for window.location.pathname