By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,968 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,968 IT Pros & Developers. It's quick & easy.

onclick

P: n/a
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>

Nov 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
eddie wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 14 '06 #2

P: n/a
eddie wrote:
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?

<snip>
<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.

Nov 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.