onclick

E

eddie

Any idea why this simple alert does not work? took this straight out of
someone elses answer to my last question ...


<html>

<head>
<title>Test</title>
<style type="text/css">
#push {
position:absolute;
background:#000;
top:50px;
width: 100px;
height: 100px;
left:100px;"
</style>
</head>

<body>

<div id="push" onclick="new Function('alert(this);')">
&nbsp;
</div>

</body>

</html>
 
M

Martin Honnen

eddie said:
Any idea why this simple alert does not work?
<div id="push" onclick="new Function('alert(this);')">

A simple alert would be
<div id="push" onclick="alert(this);">

What you have simply creates a new function with the Function
constructor. The body of the function has an alert call but the function
is nowhere called.
 
R

Richard Cornford

eddie said:
Any idea why this simple alert does not work?

It does 'work', it just doesn't do what you want it to do. But as you
are not letting on what that is you will get no closer to doing it
here.
took this straight
out of someone elses answer to my last question ...

So this had an author who you could have replied to in order to get an
answer directly?

<div id="push" onclick="new Function('alert(this);')">
<snip>

The string value of an intrinsic event attribute in HTML is used by the
browser to create a function object (as the body source code for the
function). The function object is then assigned to a property of the
DOM element that corresponds with the element in which the attribute
was declared, and executed as a method of that element when the
pertinent event happens.

This process applied to your mark-up above is approximately equivalent
of scripting:-

referenceToDivElement.onlclick = function(event){
new Function('alert(this);');
};

- that is, the onclick handler assigned to the DIV element acts by
creating a new function object and then doing nothing with that object.
Any browser you test this with almost certainly is working exactly as
programmed, but the process programmed has no visible manifestation.

Richard.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top