基本上我使用的函数如下:
function ver_pregunta(id){
$("#router").fadeOut(100).load('./includes/router.php?que=ver_pregunta&id='+id).fadeIn(1000);
return false;
}
异步加载新内容。好的,但我总是使用相同的 URL,我如何使用 JS/JQuery 更新 URL、标题和描述?
最佳答案
你只能在url中设置名称 anchor ,基本上是yourpage.com/#/someurl/ 在不重新加载整个页面的情况下不可能更改整个 url。
要设置名称 anchor ,您需要执行以下操作:
location.hash='#/my/name/anchor/path';
如果您想像这样使用它(使用名称 anchor 指向您的页面),您需要定义某种检查以使用 ver_pregunta
函数加载正确的内容,基本上获取位置.hash 例如如果您的 url = www.someurl.com/#myid
您可以运行:
if(location.hash!=""){
ver_pregunta(location.hash.substring(1));
}
或者更好地设置允许的 ID 列表:
var mypaths={"myid1":1,"myid2":1,"myid3":1}
并将其与location.hash
进行比较:
if(location.hash!="")
var lh=location.hash.substring(1);
if(mypaths[lh]==1)ver_pregunta(lh);
}
对于标题使用:
$('title').text('new title');
说明:
$('meta[http-equiv="description"]').attr('content','new description');
如果您出于 SEO 原因想要这样做,这是行不通的,Google 机器人不使用 JS,它们只会获取静态 HTML
干杯
G.
关于javascript - 如何在使用 Jquery 加载新内容时更新 URL 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4615204/