object.onmouseover

L

Laser Lips

HI All,

Is it possible to do the following?

object = document.getelementById('something');

object.onmouseover='alert("hi");';

...................


I know that object.onmouseover=function(){alert("hi");} is OK but I
want to do it the first way. Can it be done?

Graham
 
M

Martin Honnen

Laser said:
object.onmouseover='alert("hi");';

..................


I know that object.onmouseover=function(){alert("hi");} is OK but I
want to do it the first way. Can it be done?

Assigning a function makes sense, assigning a string as you do does not
make sense. So it can be done but is not in any way useful. If you have
a string then you need to create a function from it:
object.onmouseover = new Function('alert("hi");');
 
Á

Álvaro G. Vicario

Laser Lips escribió:
Is it possible to do the following?

object = document.getelementById('something');

object.onmouseover='alert("hi");'; [...]
I know that object.onmouseover=function(){alert("hi");} is OK but I
want to do it the first way. Can it be done?

Do you need to execute code from a string or is it just an aesthetics
question?

In the latter case, this is not HTML; you don't need to pack all your
code in one line. So (in my humble opinion) this looks just fine:

object.onmouseover = function(){
alert("hi");
};

You can also assign a named function:

function sayHi(){
alert("hi");
}

object.onmouseover = sayHi;


In the former case you'd have to do this:

object.onmouseover = function(){
eval('alert("hi");');
};

Not a good idea unless there's powerful reason.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top