How can I load/refer to an external CSS file inside an external JS file.
As shown in the below image I would like to refer/load Default.css inside Common.js
Thanks in advance
BB
You can't load a stylesheet "inside" JavaScript, you need to add the stylesheet to the DOM.
The link grc posted as a comment tells you how.
Create a new link
tag in script, referencing your stylesheet.
Here's how you do it in jQuery:
$("<link/>", {
rel: "stylesheet",
type: "text/css",
href: "your script here."
}).appendTo("head");
您可以通过创建一个<link>
元素并将其添加适当的属性( href
, type
等)后将其附加到document.body
来实现。
You need to try and find the absolute path of the file Default.css within the application.
From Common.js, you would be able to load Default.css using something like this:
var linkEl = document.createElement('link');
linkEl.href='/App_Themes/Default/Default.css'; // start with slash '/' for absolute path
linkEl.rel='stylesheet';
linkEl.type='text/css';
document.getElementsByTagName('head')[0].appendChild(linkEl);
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.