How to implement a remote css with javascript in any web page?

97 1

Many web sites offer browser bookmarks ( such as qq cloud input method ), and bookmark content is a javascript code that can modify the content of the current web page.

Now I want to load js to load the css files from the web site to the <head> of the current web site, so I just need to modify the remote css to control the effect, and how to introduce a remote css in js?

3 Answers

96 2
document.write('<link href="http://url/to/css" media="screen" rel="stylesheet" type="text/css"/>');

Do you want to do this? provide a point of thought, actually a dom structure, and you can modify anything with JavaScript.

62 1

document.write should only be used during document loading.
If the document is already loaded, you can replace it with the following code:

 link = document.createElement('link');
 link.href = 'http://url/to/css';
 link.media = 'screen';
 link.rel = 'stylesheet';
 link.type = 'text/css';
 document.getElementsByTagName('head')[0].appendChild(link);
126 2

It's recommended that you use this seajs.

...