getScript或eval在特定位置? [英] getScript or eval in specific location?

查看:136
本文介绍了getScript或eval在特定位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用eval(或jQuery的getScript的一些变体)来将外部javascript放置在DOM结尾或头部之外的位置。我试过:

  var head = document.getElementById(fig); 

而不是

 code> var head = document.getElementsById(head)[0]; 

  var script = document.createElement(script); 
script.text = $(。code)。val();
head.appendChild(script);

但是我似乎无法让它工作。 (代码可以正常工作,但是Firebug会显示在 #fig 之间替换的代码,而在页面的最后,紧邻< / body> 标签。



我使用的JavaScript工具包基本上是根据脚本标签所在的位置呈现的,我正在尝试要动态地修改基于用户输入的javascript(因此我不能真正引用一个新的外部JS文件 - 我宁愿运行一个eval,这不是理想的)。



我想最糟糕的情况是使用PHP或某些东西将用户输入保存到新文件中,并使用指向该新PHP文件的getScript,但是似乎非常麻烦。



再次感谢您!

解决方案

JavaScript工具包你是指使用 document.write document.writeln 将输出插入页面?如果是这样,你可以覆盖该函数以将脚本输出附加到正确的loca中

  document.write = function(s){
$('#fig')。append );
};

document.writeln = function(s){
$('#fig')。append(s +'\\\
');
};

然后使用 $。getScript

编辑:根据代码的添加方式,可以使用更强大的方法:

  var output =''; 

document.write = function(s){
output + = s;
};

document.writeln = function(s){
output + = s +'\\\
';
};

$ .getScript('script of script here',function(){
$('#fig')。append(output);
});


I was wondering if eval (or some variant of jQuery's getScript) can be used to position external javascript in places other than the end of the DOM or at the head. I've tried:

var head = document.getElementById("fig"); 

instead of

var head = document.getElementsById("head")[0];

with

var script = document.createElement("script");
script.text = $(".code").val();
head.appendChild(script);

But I can't seem to get it to work regardless. (The code does work, but Firebug shows the code being replaced both at #fig and at the end of the page, right before the </body> tag.

Basically the JavaScript toolkit I'm using renders things based on where the script tag is located, and I'm trying to dynamically modify the javascript based on user input (hence I can't really refer to a new external JS file - I'd rather run an eval, which isn't ideal).

I guess the worst case scenario would be to save the user input into a "new" file using PHP or something and use getScript pointing to that new PHP file, but it seems exceedingly hacky.

Thank you once again!

解决方案

Does the "JavaScript toolkit" you refer to use document.write or document.writeln to insert output into the page? If so, you could override that function to append the script output into the correct location:

document.write = function(s) {
    $('#fig').append(s);
};

document.writeln = function(s) {
    $('#fig').append(s + '\n');
};

and then load and execute the script using $.getScript.

Edit: A more robust way of doing it, depending on how the code is added:

var output = '';

document.write = function(s) {
    output += s;
};

document.writeln = function(s) {
    output += s + '\n';
};

$.getScript('URL of script here', function() {
    $('#fig').append(output);
});

这篇关于getScript或eval在特定位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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