最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - add a <link rel="canonical" dynamically in the head tag - Stack Overflow

programmeradmin2浏览0评论

I need to add link rel tags to the head tag dynamically based on what page is being loaded. Is there a way to do this using the page.header.controls.add method? or is it only possible through javascript or jquery? Thanks in advance for the help.

I need to add link rel tags to the head tag dynamically based on what page is being loaded. Is there a way to do this using the page.header.controls.add method? or is it only possible through javascript or jquery? Thanks in advance for the help.

Share Improve this question asked Dec 1, 2014 at 5:53 ManiMani 3791 gold badge5 silver badges22 bronze badges 3
  • would probably be best to do this server side with whatever CGI programming language you prefer. In this case you'll use ASP. – Rafael Commented Dec 1, 2014 at 5:57
  • thanks for the reply, i am a newbie, could you please elaborate? much appreciated – Mani Commented Dec 1, 2014 at 6:00
  • 1 I say it is best to use server side for this because if a request is made by a user you want the server to process the output before the user gets it. This would make more sense to do in a server language in my opinion. – Rafael Commented Dec 1, 2014 at 6:40
Add a ment  | 

3 Answers 3

Reset to default 3

You can use HtmlGenericControl class for this:-

HtmlGenericControl linkFile = new HtmlGenericControl("link");
linkFile.Attributes.Add("rel", "canonical");
linkFile.Attributes.Add("href", "testPath");
Page.Header.Controls.Add(linkFile);

Can you try this

if(!document.getElementById('id2')) { var link = document.createElement('link'); link.id = 'id2'; link.rel = 'stylesheet'; link.href = 'CSS/Css1.cs'; document.head.appendChild(link); }

We can also use this:

 HtmlLink clink = new HtmlLink();
    clink.Attributes.Add( HtmlTextWriterAttribute.Rel.ToString().ToLower(), "canonical");
    clink.Href = "http://www.test.co.in/";
    Page.Header.Controls.Add(clink);
发布评论

评论列表(0)

  1. 暂无评论