Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to dynamically load the URLs in an HTML head tag?

I’m trying to load my CSS and JS files based on a localStorage variable.

It looks just like this:

let jx = localStorage.getItem('jurisdiction');

document.write(`

<link rel="stylesheet" href="../_externals/stylesheets/assist/${jx}.css">
    
 `);

However this doesn’t seem to work and I can’t find a solution how to dynamically change a URL in the head tag.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’d appreciate any help as to how to go about this. Thank you!

>Solution :

I think that href in your tag should look like this:
href=../_externals/stylesheets/assist/${jx}.css

Notice that I have used template literals(“) to wrap the link in ‘href’ attribute.
In order to use the ${jx} inside your link, you need to wrap the whole link inside the template literals(e.g. text${jsVariables}). I think this should work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading