I’m modifying some legacy code and I found a web page that loads the JQuery library only to perform the following (this is inside a <script> tag in the <body> of the page):
$(document).ready(function(){
//Once the document is ready, run the following code
$("head").append($("<link rel='stylesheet' href='style-"+myCSS+".css' type='text/css' media='screen' />"));
});
I want to convert this code to regular JavaScript and remove JQuery (I’m not against the use of JQuery, I’m against the idea of having to load 90+Kb only to do that).
My idea, but I’m not a JS expert, is to use (in the same position in the page):
headReference.innerHTML = "<link rel='stylesheet' href='style-" + myCSS + ".css' type='text/css' media='screen' />";
Is there any better solution?
Any help is appreciated.
Here’s the equivalent plain javascript:
If it matters to load that after the document has loaded (which is probably not required), then you can place this code in a script tag right before the end of the
<body>tag – otherwise, you can put it anywhere in the<body>element.