使用Jquery加载新内容时如何更新URL地址

时间:2011-01-06 13:06:48

标签: javascript jquery html url title

基本上我使用的功能如下:

function ver_pregunta(id){
        $("#router").fadeOut(100).load('./includes/router.php?que=ver_pregunta&id='+id).fadeIn(1000);
        return false;
    }

加载新内容异步。好吧,但我总是使用相同的URL,我怎样才能用JS / JQuery更新URL,TITLE和DESCRIPTION?

3 个答案:

答案 0 :(得分:2)

你只能在网址中设置名称锚点基本上是yourpage.com/#/someurl/ 如果不重新加载整个页面,就无法更改整个URL。

要设置名称锚点,您需要执行以下操作:

location.hash='#/my/name/anchor/path';

如果您想这样使用它(指向带有名称锚点的页面),您需要定义某种检查以使用ver_pregunta函数加载正确的内容,基本上采用location.hash,例如如果您的网址= 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');

如果你想出于搜索引擎优化的原因这样做是行不通的,谷歌机器人不使用JS,他们只会选择静态HTML

干杯

-G。

答案 1 :(得分:0)

window.location = "#newurl";
document.title = 'new title'; 
$('meta[name=description]').attr("content", "new description")

答案 2 :(得分:0)

使用javascript的原生窗口功能。

window.location.replace('http://www.google.com/')