开发者

how to update URL adress when loading new content with Jquery

开发者 https://www.devze.com 2023-02-02 14:55 出处:网络
basically i use functions like: function ver_pregunta(id){ $(\"#router\").fadeOut(100).load(\'./includes/router.php?que=ver_pregunta&id=\'+id).fadeIn(1000);

basically i use functions like:

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

to load new content asynch. ok, but i'm allways using same URL, how Can i ju开发者_Go百科st update URL, TITLE and DESCRIPTION with JS/JQuery?


you can only set the name anchor in the url basically yourpage.com/#/someurl/ it's not possible to change the whole url without reloading the whole page.

To set the name anchor you need to do this:

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

If you want to use it like that (to point to your page with name anchor) you'll need to define some kind of check to load proper content with ver_pregunta function, basically take the location.hash e.g. if your url = www.someurl.com/#myid you can run:

if(location.hash!=""){
   ver_pregunta(location.hash.substring(1));
}

or better set a list of allowed ids:

var mypaths={"myid1":1,"myid2":1,"myid3":1}

and compare it with location.hash:

if(location.hash!="")
  var lh=location.hash.substring(1);
  if(mypaths[lh]==1)ver_pregunta(lh);
}

For title use:

$('title').text('new title');

For description:

$('meta[http-equiv="description"]').attr('content','new description');

If you want to do it for SEO reasons it won't work, Google bots don't use JS, they'll pick up only static HTML

Cheers

G.


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


Use javascript's native window function.

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

精彩评论

暂无评论...
验证码 换一张
取 消