Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append some HTML into the HEAD tag?

I want to add some style to head tag in html page using javascript.

var h = document.getElementsByTagName('head').item(0);
h.innerHTML += '<style>a{font-size:100px;}</style>';

But when I run this code in IE8 I see this error message: Could not set the innerHTML property. Invalid target element for this operation.

Any ideas?

like image 497
Aleksandr Ivanov Avatar asked Apr 20 '10 15:04

Aleksandr Ivanov


People also ask

How to add a <head> tag inside of an HTML tag?

Add the opening and closing <head> tags inside of the <html> tags. Next, add two additional lines of HTML code inside the <head> tags like this: Note that you have nested a variety of HTML elements inside one another. The <title> and <meta> elements are nested inside the <head> element, and the <head> element is nested inside the <html> element.

What is the use of head in HTML?

HTML <head> Tag 1 Definition and Usage. The <head> element is a container for metadata (data about data) and is placed between the <html> tag and the <body> tag. 2 Browser Support 3 Global Attributes. The <head> tag also supports the Global Attributes in HTML. 4 More Examples 5 Related Pages 6 Default CSS Settings

How to add additional lines of HTML code inside a tag?

Next, add two additional lines of HTML code inside the <head> tags like this: Note that you have nested a variety of HTML elements inside one another. The <title> and <meta> elements are nested inside the <head> element, and the <head> element is nested inside the <html> element. We will nest elements frequently as the tutorial proceeds.

What is an example of a title tag in HTML?

Example. A simple HTML document, with a <title> tag inside the head section: <!DOCTYPE html>. <html lang="en">. <head>. <title> Title of the document </title>. </head>. <body>. <h1> This is a heading </h1>.


1 Answers

Create the style element with createElement:

var h = document.getElementsByTagName('head').item(0);
var s = document.createElement("style");
s.type = "text/css"; 
s.appendChild(document.createTextNode("a{font-size:100px;}"));
h.appendChild(s);
like image 170
RoToRa Avatar answered Sep 29 '22 01:09

RoToRa