Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add DOM element script to head section?

I want to add DOM element to head section of HTML. jQuery does not allow adding DOM element script to the head section and they execute instead, Reference.

I want to add script tags and write a script within <head> section.

var script = '<script type="text/javascript"> //function </script>' $('head').append(script); 

Something like this with functions. I tried jQuery and javascript\, but it does not work.

Please tell me how to add and write script to head by jQuery or javascript.

I tired the javascript to add DOM element, but it does not work with .innerHTML() to write to head. I am using jQuery 2.0.3 and jQuery UI 1.10.3.

I want to add base64 encoded script to head section. I use base64 decoder js like this to decode the javascript and then put on the head section.

//Edited
It will be

$.getScript('base64.js'); var encoded = "YWxlcnQoImhpIik7DQo="; //More text var decoded = decodeString(encoded); var script = '<script type="text/javascript">' +decoded + '</script>'; $('head').append(script); 

To fit an encoded script and the addition in one javascript file. I want to use base64.js or some other decoder javascript files for browsers does not accept atob().

like image 284
john3825 Avatar asked Sep 13 '13 11:09

john3825


People also ask

How do you add a script to a head tag?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Can you put the script element inside the head >?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Can you put JavaScript in head?

JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body. JavaScript in head: A JavaScript function is placed inside the head section of an HTML page and the function is invoked when a button is clicked.


1 Answers

try this

var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'url';      document.head.appendChild(script); 
like image 70
pbaris Avatar answered Sep 20 '22 23:09

pbaris