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

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

问题描述

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

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.

我想添加 script 标签并在 部分编写一个脚本.

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 添加和编写 script 到 head.

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 编码的脚本添加到 head 部分.我使用像 this 这样的 base64 解码器 js 来解码 javascript,然后放在 head 部分.

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.head.appendChild(script);

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

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