Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML: Adding <STYLE> and <SCRIPT> tags via JavaScript

If a JavaScript function in a web page adds STYLE or SCRIPT elements to the HEAD element, will the browser load the specified styles and scripts, or will such programmatic changes to the DOM ignored after the page has been loaded?

Is the behavior consist across all mainstream browsers?

like image 451
Tony the Pony Avatar asked May 27 '11 14:05

Tony the Pony


People also ask

How do you style a HTML tag in JavaScript?

There are two other ways to set style using JavaScript. The first is to use the setAttribute() method. The second is by adding a class using the add() method of the classList property. The class you add can then be handled using external CSS styling.

Can we add styles using JavaScript?

By using the JavaScript DOM methods, you can add the inline styles or global styles to the element.

Where do you put the script and style tag in HTML?

The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load. Generally, JavaScript code can go inside of the document <head> section in order to keep them contained and out of the main content of your HTML document.


1 Answers

Yes, you can add scripts and stylesheets via JavaScript, and they will work. Google Analytics does this, for example.

like image 80
Will Martin Avatar answered Oct 18 '22 00:10

Will Martin