1

I have a custom ASP.Net control that has a function name to be attached inside a javascript object:

<custom:myControl runat="server" id="something" OnClientOpened="myFunc" />

I then have a state javascript object that has the string "myFunc":

state.onClientOpened // contains "myFunc"

I am binding a function to the slideUp argument like this:

// this code is inside an Object
// Simplified for clarity

$el.slideUp(200, window[state.onClientOpened]);

I have no control over what goes into the OnClientOpened property so then I tested something that wasnt in the global namespace:

var MyObject = function() {

    var myFunc = function() {
        doSomething();
    };

    return {
        myFunc: myFunc
    };
}();

The state.onClientOpened would now contain:

onClientOpened = "MyObject.myFunc";

This obviously breaks it. Whats the best approach for this? I'm thinking some sort of eval but I'm hoping for another approach if possible.

Eval solution:

$el.slideUp(200, eval("window." + state.onClientOpened));
1
  • Did my last update answer your question? Commented Jan 17, 2014 at 4:15

3 Answers 3

2

Just wrap all your code in an anonymous function, not part of it, then you don't have to worry about global namespace pollution. And you don't need to reference the whole window rigamarole, just refer to the function as the callback itself.

(function () {

  var myFunc = function() {
    doSomething();
  };

  // Simplified for clarity
  $el.slideUp(200, myFunc);

})();

Approach 2

var MyObject = {

  myFunc: function() {
    doSomething();
  }

};

onClientOpened = MyObject.myFunc;

Approach 3

// approach 2, but with this change:
onClientOpened = MyObject["myFunc"];

// or if MyObject also comes from the server,
onClientOpened = window["MyObject"]["myFunc"]
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you but I need to keep the approach I'm showing, I've commented a lot of code that would explain why.
Okay I added a second approach you can try.
That would work but the string comes from the server, which is why I can't use that.
Then using approach2: onClientOpened = MyObject[someVariableWithFuncName];
Exactly what I was thinking, @itsmejodie
|
0

The variable attached to the window namespace is just MyObject, not myFunc. If you want to access the function, try doing it like so:

window["MyObject"]["myFunc"];

You can split the string coming in from the server by using split.

var params = onClientOpened.split(".");

 $el.slideUp(200,window[params[0]][params[1]]);

2 Comments

Actually, I am self instantiating the MyFunc object so it does work like so.
I see, new solution. Tested in Chrome JS console.
0

Here are the options I can see so far.

Approach 1 (eval):

$el.slideUp(200, eval("window." + state.onClientOpened));

Approach 2 (Parse it):

var onclientOpened;
var splitArray = state.onclientOpened.split(".");

for (var i = 0; i < splitArray.length; i++) {
    if (i == 0) {
        onclientOpened = window[splitArray[i]];
    } else {
        onclientOpened = onclientOpened[splitArray[i]];
    }
}

$el.slideUp(200, onclientOpened);

Approach 2 seems to be the "safest" one, but would appreciate other alternatives.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.