Netorica
Netorica

Reputation: 19347

Evaluate a JQUERY statement

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

function 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 =)

Upvotes: 1

Views: 150

Answers (2)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

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

}

Upvotes: 2

Guard
Guard

Reputation: 6955

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

then, wrap it like that:

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

Upvotes: 0

Related Questions