Working with CMS, which prevents editing HTML source for <head>
element.
For example I want to add the following above the <title>
tag:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
Working with CMS, which prevents editing HTML source for <head>
element.
For example I want to add the following above the <title>
tag:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
Create a temporary element (e. g.
DIV
), assign your HTML code to itsinnerHTML
property, and then append its child nodes to theHEAD
element one by one. For example, like this:Compared with rewriting entire
HEAD
contents via itsinnerHTML
, this wouldn’t affect existing child elements of theHEAD
element in any way.Note that scripts inserted this way are apparently not executed automatically, while styles are applied successfully. So if you need scripts to be executed, you should load JS files using Ajax and then execute their contents using
eval()
.JavaScript:
Make DOM element like so:
You can select it and add to it as normal:
In the latest browsers (IE9+) you can also use document.head:
Example:
You can use
innerHTML
to just concat the extra field string;However, you can't guarantee that the extra things you add to the head will be recognised by the browser after the first load, and it's possible you will get a FOUC (flash of unstyled content) as the extra stylesheets are loaded.
I haven't looked at the API in years, but you could also use
document.write
, which is what was designed for this sort of action. However, this would require you to block the page from rendering until your initial AJAX request has completed.i don't know why i hate using jquery. i gave head an id of 'head'.