admin管理员组

文章数量:1332383

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);

本文标签: javascriptadd a ltlink relquotcanonicalquot dynamically in the head tagStack Overflow