Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   object.onmouseover (http://www.velocityreviews.com/forums/t935928-object-onmouseover.html)

Laser Lips 05-21-2008 02:58 PM

object.onmouseover
 
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

Martin Honnen 05-21-2008 03:01 PM

Re: object.onmouseover
 
Laser Lips wrote:

> 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");');


--

Martin Honnen
http://JavaScript.FAQTs.com/

Laser Lips 05-21-2008 03:10 PM

Re: object.onmouseover
 
Thats just what I needed thanks. :0)
Graham

Álvaro G. Vicario 05-21-2008 03:30 PM

Re: object.onmouseover
 
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.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--


All times are GMT. The time now is 10:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.