admin管理员组

文章数量:1134592

I'm working with a 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" />

I'm working with a 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" />
Share Improve this question edited May 29, 2020 at 21:34 cobrexus 4,7885 gold badges23 silver badges49 bronze badges asked Dec 14, 2009 at 13:17 Jitendra VyasJitendra Vyas 152k238 gold badges583 silver badges865 bronze badges 6
  • 8 This doesn't make sence... The head is parsed prior to the execution of javascript. Adding meta stuf to the head via javascript would not have the desired effect. – Andre Haverdings Commented Dec 14, 2009 at 13:32
  • 1 @Mickel - yes. answers of all questions helps me – Jitendra Vyas Commented Dec 14, 2009 at 15:09
  • 2 While not related to the CMS question, it can make sense to add meta tags in certain circumstances. There are various browser addons and javascript injections that use data that is in the meta tags to gather information. Facebook's OpenGraph is one example. Injecting meta tags into the head is needed when you don't have direct access to the originating HTML, whether by fault of a CMS or because you are writing a javascript addon/injection yourself. – conceptDawg Commented Jan 31, 2011 at 23:07
  • Note that it's possible that adding <meta> tags dynamically will have no effect, depending on what they are and what browser is involved. – Pointy Commented May 9, 2016 at 14:18
  • Good point, that's what happens when one focuses too much on the problem ;-) – mb897038 Commented May 9, 2016 at 14:21
 |  Show 1 more comment

8 Answers 8

Reset to default 173

You can select it and add to it as normal:

$('head').append('<link />');

JavaScript:

document.getElementsByTagName('head')[0].appendChild( ... );

Make DOM element like so:

const link = document.createElement('link');
link.href = 'href';
link.rel = 'rel';

document.getElementsByTagName('head')[0].appendChild(link);

jQuery

$('head').append( ... );

JavaScript:

document.getElementsByTagName('head')[0].appendChild( ... );

You can use innerHTML to just concat the extra field string;

document.head.innerHTML = document.head.innerHTML + '<link rel="stylesheet>...'

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.

In the latest browsers (IE9+) you can also use document.head:

Example:

var favicon = document.createElement('link');
favicon.id = 'myFavicon';
favicon.rel = 'shortcut icon';
favicon.href = 'http://www.test.com/my-favicon.ico';

document.head.appendChild(favicon);

Create a temporary element (e. g. DIV), assign your HTML code to its innerHTML property, and then append its child nodes to the HEAD element one by one. For example, like this:

var temp = document.createElement('div');

temp.innerHTML = '<link rel="stylesheet" href="example.css" />'
               + '<script src="foobar.js"><\/script> ';

var head = document.head;

while (temp.firstChild) {
    head.appendChild(temp.firstChild);
}

Compared with rewriting entire HEAD contents via its innerHTML, this wouldn’t affect existing child elements of the HEAD 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().

Try a javascript pure:

Library JS:

appendHtml = function(element, html) {
    var div = document.createElement('div');
    div.innerHTML = html;
    while (div.children.length > 0) {
        element.appendChild(div.children[0]);
    }
}

Type: appendHtml(document.head, '<link rel="stylesheet" type="text/css" href="http://example.com/example.css"/>');

or jQuery:

$('head').append($('<link rel="stylesheet" type="text/css" />').attr('href', 'http://example.com/example.css'));

With jquery you have other option:

$('head').html($('head').html() + '...');

anyway it is working. JavaScript option others said, thats correct too.

本文标签: How to add anything in ltheadgt through jqueryjavascriptStack Overflow