简体   繁体   English

javascript 传递 eval 变量

[英]javascript pass eval variables

i have eval function, which needs to execute javascript from php.我有 eval 函数,它需要从 php 执行 javascript。 but i need to pass element, so i can put mouse over tips on the link user clicked on.但我需要传递元素,所以我可以将鼠标放在用户点击的链接上的提示上。

var globalEval = function globalEval(src, element) {
        if (window.execScript) {
            window.execScript(src);
            return;
        }
        var fn = function(element) {
            window.eval.call(window,src);
        };
        fn(element);
    };

im using following way to pass $(this) element我使用以下方式传递$(this)元素

globalEval(js_code, $(this));
// js_code is = alert(element);

i get error of undefined element, which is defined in globalEval();我得到未定义元素的错误,这是在globalEval();定义的globalEval(); how can i fix this?我怎样才能解决这个问题?

This is a scoping issue as the global eval isn't invoking the code in the same scope as the variable element .这是一个范围问题,因为全局 eval 不会在与变量element相同的范围内调用代码。 If you must use eval even though eval is evil , you'll have to do it in a way that lets you invoke your code in the environment you want.如果您必须使用eval即使eval 是 evil ,您也必须以一种允许您在所需环境中调用代码的方式来使用它。 One way to do this is to wrap it as an anonymous function which you give parameters for the environment variables of choice.一种方法是将其包装为匿名函数,您可以为所选的环境变量提供参数。

For example例如

window.eval.call(window,'(function (element) {'+src+'})')(element);

This means the src string is parsed but not invoked by the eval as it returns an anonymous function.这意味着src字符串被解析但不被eval调用,因为它返回一个匿名函数。 You then invoke it, passing your data, in this case element .然后调用它,传递数据,在本例中为element

Test it with var element = document.body, src = 'console.log(element.tagName)';var element = document.body, src = 'console.log(element.tagName)';测试一下var element = document.body, src = 'console.log(element.tagName)'; and you'll see it log "BODY" .你会看到它记录了"BODY" Please note that if you want to set global variables (or functions) this way they have to be stated as global explicitly ( window.foobar = ... ) or they will be GCd after the anonymous function finishes.请注意,如果您想以这种方式设置全局变量(或函数),它们必须显式声明为全局变量( window.foobar = ... ),否则在匿名函数完成后它们将被 GCd。

If all you want to do is have this set when you evaluate some code, try:如果想要做的是有this当你评估一些代码集,请尝试:

// Code you want to evaluate
var code = 'return this.whatever'

// What you want "this" bound to:
var that = { whatever: 69 }

// Now do this:
var result = new Function(code).call(that)

Using the Function constructor means you'll get what you expect;使用 Function 构造函数意味着你会得到你所期望的; There is a lot of baggage that comes along with global eval, some of which might surprise you. global eval 伴随着很多包袱,其中一些可能会让您感到惊讶。 Best to avoid it if you don't need it.如果你不需要它,最好避免它。

Now if you really wanted to call it element , the Function constructor can do that as well:现在,如果您真的想将其称为element ,则Function 构造函数也可以这样做:

code = 'alert(element)'
var use_element = 69
result = new Function("element", code).call(this, use_element)

if the callback function is in the same dom, then set a global variable as repo before eval which will keep the variable.如果回调函数在同一个 dom 中,则在 eval 之前将全局变量设置为 repo,这将保留该变量。 then use the global variable in the function you gonna call.然后在您要调用的函数中使用全局变量。

var response_repo;
function someFunc($callback){
 ....
 success: function(result) {
            response_repo = result;
            eval($callback+'();');
        },
 ....
 }


 callBackFunc(){
  $data=response_repo;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM