Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:12:02+00:00 2026-06-16T00:12:02+00:00

i have eval function, which needs to execute javascript from php. but i need

  • 0

i have eval function, which needs to execute javascript from php. 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

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

i get error of undefined element, which is defined in globalEval(); how can i fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:12:02+00:00Added an answer on June 16, 2026 at 12:12 am

    This is a scoping issue as the global eval isn’t invoking the code in the same scope as the variable 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. 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. You then invoke it, passing your data, in this case element.

    Test it with var element = document.body, src = 'console.log(element.tagName)'; and you’ll see it log "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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've often heard the argument (in javascript, but many languages have an eval-like feature)
I have some 100 files in which I need to replace: Eval(something) with Eval(something).ToEncodedString()
Possible Duplicate: How to execute a JavaScript function when I have its name as
I have a javascript function which asks for some ajax data and gets back
I have some javascript: eval('var incomingJSON =' + ajaxObj.responseText); for (p in incomingJSON.common) {
I have some lisp initialisation code: (eval-when (:compile-toplevel :load-toplevel :execute) (require 'asdf)) (eval-when (:compile-toplevel
I have ImageButton with CommandArgument attribute which is having multiple Eval value. When I
Say, if I have a Lisp program, which uses (eval 'sym) and looks it
I have in a json object which stores a JavaScript say var jsonObj =
I have a function which calls several other functions, e.g. sub do_work { send_mail();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.