Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How to add CSS Link dynamically to elements?

$('head').append('<link type="text/css" rel="stylesheet" href="my css path">');

The below code doesnt work because we need to specify the url/path of the css file.

$('head').append('<SharePoint:CssLink runat=”server” Version=”4″ />');
share|improve this question
    
In my suggestion, you should use delegate control. –  Amit Maheshwari Aug 3 at 8:25

3 Answers 3

up vote 0 down vote accepted

The CssLink-Control is a server-side control. Jquery is only client-side, you can´t do that using jquery.

share|improve this answer

The other option can be to add a custom action (A scriptlink) Using SharePoint JSOM

share|improve this answer

Here's the "oldschool" way of doing it, which hopefully works accross all browsers. In theory you would use setAttribute unfortunately IE6 doesn't support it consistently

var cssId = 'myCss';  // you could encode the css path itself to generate id..
if (!document.getElementById(cssId))
{
    var head  = document.getElementsByTagName('head')[0];
    var link  = document.createElement('link');
    link.id   = cssId;
    link.rel  = 'stylesheet';
    link.type = 'text/css';
    link.href = 'http://website.com/css/stylesheet.css';
    link.media = 'all';
    head.appendChild(link);
}

This example checks if the CSS was already added so it adds it only once.

Put that code into a javascript file, have the end-user simply include the javascript, and make sure the CSS path is absolute so it is loaded from your servers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.