开发者

printing a url with variables

TLDR

I want to print this

<link rel="canonical" href="http://nyc.mymusicwebsite.com/coolband/1222
"/>开发者_开发问答;

where

var termsText = nyc
var deliurl = http://nyc.mymagazine.com/coolband/122
deliurl2[1] = mymagazine
deliurl[2] = .com/coolband/122

(deliurl is split at .) I have this code so far which almost works, This is the problem

document.write('<link rel=cononical"'+" ""href="http:"//"""+termsText+"."+deliurl2[1]+deliUrl[2]'"/>');

I have a drupal website that creates duplicate content for every post,

every post creates multiple pages

http://nyc.mymusicwebsite.com/coolband/1222
http://national.mymusicwebsite.com/coolband/1222
http://seatle.mymusicwebsite.com/coolband/1222
http://la.mymusicwebsite.com/coolband/1222

though only one post is visible on the website the other posts are getting indexed.

the posts above are all tagged in a div with the region they are assigned and visible on the website.

I want to print the google rel conical link, linking to the conincal post. this post would have the subdomain identical to the pages tag in the div.

I have done everything except printing the url

consider the following code

    //find tag in page for pattern match
    var termsText = $("#terms").find("ul li:last").text(); 

    //get window location url to test agains pattern
    var deliUrl=window.location.href


    //test pattern
    var patt1=new RegExp(termsText);


    //split url at first period to isolate everything after the subdomain
    var deliUrl2=deliUrl.split('.');


    //if pattern is NOT in url print google conical tag pointing to propper subdomain
    if (patt1.test(deliUrl));

    {
        //print subdomain as "terms" and rest of url
        document.write('<link rel=cononical"'+" ""href="+termsText+"."+deliurl2[1]+deliUrl[2]'"/>');

}


See this fiddle - http://jsfiddle.net/TDCEN/1/

I used your hardcoded values. There were a number of things wrong in your document.write method. Javascript is case sensitive so you have to make sure things match. Be careful with your quotes. You can go multiline in js, so do as I did and break things up in a sensical manner. I changed the first < to > because it otherwise the output could not be shown.

I still don't think this will work though. I doubt the search engines are reading head information modified by js. Good luck though. Definitely look into robots.txt file. You can basically say don't index these pages. This affects SEO since they won't be index at all, but that sounds like what you want.

var termsText = 'nyc';
var deliurl = 'http://nyc.mymagazine.com/coolband/122';
var part1 = 'mymagazine';
var part2 = '.com/coolband/122';

document.write(
    '>link rel="canonical" href="http://' +
    termsText +
    '.' +
    part1 +
    part2 +
    '" />'             
);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜