admin管理员组文章数量:1356905
...with right path.
For example. I have script called foo.js. I'd like to insert stylesheet declaration which I can do with following instruction:
$('head').append('<link rel="stylesheet" href="/template/foo.css" type="text/css" />');
Problem: I have to put full path to the stylesheet file. So instead of /template/foo.css I have to put: http://hostname/directory/template/foo.css. I can't set it statically beacause script can be placed in different servers and different locations. So it can be: .css or .css.
It would be very useful if I can get path of foo.js file on the server. That would be good enough beacause then I could set stylesheet location based on the javascrpt's file.
...with right path.
For example. I have script called foo.js. I'd like to insert stylesheet declaration which I can do with following instruction:
$('head').append('<link rel="stylesheet" href="/template/foo.css" type="text/css" />');
Problem: I have to put full path to the stylesheet file. So instead of /template/foo.css I have to put: http://hostname/directory/template/foo.css. I can't set it statically beacause script can be placed in different servers and different locations. So it can be: http://foo./bar/foo.css or http://foo./foo.css.
It would be very useful if I can get path of foo.js file on the server. That would be good enough beacause then I could set stylesheet location based on the javascrpt's file.
Share Improve this question asked Jan 9, 2010 at 14:56 BaldBald 2,3363 gold badges27 silver badges33 bronze badges 4- I have noticed that TinyMCE does that – Bald Commented Jan 9, 2010 at 14:58
- Is there a definite relationship between the locations of foo.js and foo.css, such as the same directory? Or perhaps between the html page and the css file? – Justin Love Commented Jan 9, 2010 at 15:34
- is there some kind of logic of where to find the css file ? – Gabriele Petrioli Commented Jan 9, 2010 at 16:09
- Yes. Let's just say that foo.js and foo.css are in the same directory. – Bald Commented Jan 10, 2010 at 12:35
3 Answers
Reset to default 7I've always done:
$('body').append('<link rel="stylesheet" href="/template/foo.css" type="text/css" />');
instead of head
Ahh... sorry I just realised what your problem is. One strategy is to extract the path of the script from the DOM itself:
$('script').each(function(i,el){
var path = el.src.match(/^(.+)\/foo.js$/);
if (path) {
$('body').append('<link rel="stylesheet" ' +
'href="' + path[1] + '/foo.css" ' +
'type="text/css" />'
);
}
})
This is a mon technique that I use to get the current script url:
var scriptUrl = (function() {
var scripts = document.getElementsByTagName('script'),
script = scripts[scripts.length - 1];
return script.src;
})();
It works basically because when the script is being executed, it is the last script
tag on the DOM.
You can use window.location
and obtain the path from that. For example for this page it is:
>>> window.location
http://stackoverflow./questions/2033742/how-to-insert-external-stylesheet-in-javascript-dynamically
本文标签: How to insert external stylesheet in JavaScript (dynamically)Stack Overflow
版权声明:本文标题:How to insert external stylesheet in JavaScript (dynamically) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743958235a2568529.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论