Mani
Mani

Reputation: 379

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.

Upvotes: 4

Views: 5240

Answers (3)

Mani
Mani

Reputation: 379

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

Upvotes: 0

Rahul Singh
Rahul Singh

Reputation: 21825

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

Upvotes: 3

Akshay Randive
Akshay Randive

Reputation: 384

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

Upvotes: 3

Related Questions