如何在头部添加DOM元素脚本? [英] How to add DOM element script to head section?

查看:710
本文介绍了如何在头部添加DOM元素脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将DOM元素添加到HTML的头部。 jQuery不允许在头部添加DOM元素脚本,而是执行参考

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.

我想添加脚本标签,并在< head> 部分。

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

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

这样有功能的东西。我试过jQuery和javascript\,但是它不起作用。

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

请告诉我如何添加和写入脚本由jQuery或javascript头。

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

我累了javascript添加DOM元素,但它不适用于 .innerHTML()写头。我正在使用jQuery 2.0.3和jQuery UI 1.10.3。

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.

我想将base64编码的脚本添加到头部。我使用base64解码器js,如解码JavaScript,然后放在头部。

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);

将一个编码的脚本和添加到一个javascript文件中。
我想使用 base64.js 或浏览器的其他解码器javascript文件不接受 atob()

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().

推荐答案

尝试这个

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'url';    

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

这篇关于如何在头部添加DOM元素脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆