srijan
srijan

Reputation: 1522

Executing the content of string variable as line of code in javascript?

I have something like

"_onmouseover" : "this.className=this.className.replace(' hover', '')";

I'm trying to execute it like

buttonObject.onmouseover = function( ) { window [ this.someObject.__onmouseover ] () ; };

And I don't know how it is possible.

Let me tell you guys my scenario. I am creating this plugin to generate four types of dialogue messages in a jquery dialogue box. Those being 'Warning', 'Error', 'Note' and 'Confirm'. So lets say there are 4 spans in dom which should trigger these four.

<span id='DialogueWarning'> Warning </span> 
<span id='DialogueError'> Error </span> 
<span id='DialogueNote'> Note </span> 
<span id='DialogueConfirm'> Confirm </span>

Now lets hadle the click to show the dialogue

jQuery('#DialogueWarning').click(function(){

        var dialogue = new Dialogue({
            "type":"Warning",
            "message":"Are you sure you want to close this window without saving your changes?",
            "buttons":
            [
                {   
                    "text":"Close without saving", 
                    "_onmouseover": "this.className+=' hover'",
                    "_onmouseout":"this.className=this.className.replace(' hover', '')",
                    "bClass":"e_customButton"
                },

                {   
                    "text":"Don't Close",
                    "_onmouseover": "this.className+=' hover'",
                    "_onmouseout":"this.className=this.className.replace(' hover', '')",
                    "bClass":"e_customButton"
                }
            ],
            "closeOnBackgroundClick" : true
        });
    });

See the "_onmouseover" and _onmouseout thingy, I need those. Is there any way I can pass those in another way

Upvotes: 1

Views: 229

Answers (3)

Anoop
Anoop

Reputation: 23208

You can use Function . demo

buttonObject.onmouseover = Function(window [ this.someObject.__onmouseover ] );

Upvotes: 0

Eugene Naydenov
Eugene Naydenov

Reputation: 7295

If you need an eval, I bet you have some problems in your application's design.
E.g. you can avoid such things:

// ...
var eventHandlers = {
    "_onmouseover" : "this.className=this.className.replace(' hover', '')"
};

// ...

eval(eventHandlers._onmouseover);

and just do it like

var eventHandlers = {
    _onmouseover: function(e) {
        this.className=this.className.replace(' hover', '');
    }
};

buttonObject.onmouseover = eventHandlers._onmouseover;

Some articles to read:

# 1
# 2
# 3

Upvotes: 1

dqhendricks
dqhendricks

Reputation: 19251

Why does it have to be a string in the first place?

If you had something like:

var someObject = {
   _onmouseover: function() {
      this.className = this.className.replace(' hover', '');
   }
}

You could execute it like:

buttonObject.onmouseover = someObject.__onmouseover;

If you need this to be the button object, you might do something like this:

buttonObject.onmouseover = function() {
   someObject.__onmouseover.call( buttonObject );
};

Upvotes: 0

Related Questions