1

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

1
  • Can you show handling of buttons in Dialogue? Commented Sep 20, 2012 at 17:24

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

8 Comments

I think the onclick string is coming from a JSON-encoded reply or somesuch.
@Abody97 what leads you to believe that?
"_onmouseover" : "this.className=this.className.replace(' hover', '')"; looks JSON-ish.
@Abody97 haha every object literal in javascript looks "JSONish".
@Abody97 Yes, but perhaps only because he/she doesn't know about assigning anonymous functions to properties. Hence my suggestion.
|
0

You can use Function . demo

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.