简体   繁体   中英

add a <link rel=“canonical” dynamically in the head tag

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.

你能试试吗?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM