开发者

Evaluate a JQUERY statement

开发者 https://www.devze.com 2023-04-08 18:25 出处:网络
I use this kind of script to evaluate my javascript code that is injected in the DOM funct开发者_如何转开发ion wilEval(source) {

I use this kind of script to evaluate my javascript code that is injected in the DOM

funct开发者_如何转开发ion wilEval(source) {

  if ('function' == typeof source) {
    source = '(' + source + ')();'
  }
  var script = document.createElement('script');
  script.setAttribute("type", "text/javascript");
  script.textContent = source;
  document.body.appendChild(script);

  if (window.execScript) {
         window.execScript(source);
  }

}

it works in IE and other major browsers but my problem is the code to be evaluated is a jquery code like this $("#<?php echo "utm".$thr_id; ?>").effect("highlight", {}, 1000); So how can evaluate it like a normal javascript code? thanks

P.S. the php echo just produce a dynamic element id =)


One thing you could do is use globalEval()

function wilEval(source) {

  if ('function' == typeof source) {
    source = '(' + source + ')();'
  }


  if (window.execScript) {
     var script = document.createElement('script');
     script.setAttribute("type", "text/javascript");
     script.textContent = source;
     document.body.appendChild(script);
     window.execScript(source);
  }else{
     jQuery.globalEval(source);
  }

}


so, your code is not a function but rather a statement

then, wrap it like that:

(function () { YOUR STATEMENT }());
0

精彩评论

暂无评论...
验证码 换一张
取 消