Welcome Guest Search | Active Topics | Sign In | Register

Scope of variables in EvalScript Options
BenjaminSimpson1989
Posted: Tuesday, March 19, 2019 4:57:01 PM
Rank: Advanced Member
Groups: Member

Joined: 1/12/2015
Posts: 81
What's the scope of declared variables in EvalScript? Can I access global variables defined in the Javascript that runs from the loaded webpage? If I do "var myVar" in one EvalScript, does it create a global variable called myVar? If not, can I still reference it from another call to EvalScript later in my code?
eo_support
Posted: Tuesday, March 19, 2019 5:30:10 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,072
EvalScript runs in the global scope. So when you use "var myVar", it does create global variable myVar. If that's not desired, you can use local function to limit the scope. For example:

Code: C#
EvalScript(@"
var evalFx = function() 
{ 
    var priceBox = document.getElementById('priceBox'); 
    return priceBox == null || priceBox.offsetParent == null; 
};

evalFx();");


The above code defines an anonymous function evalFx and then call it. variable "evalFx" is still global but all other variables are inside the anonymous function.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.