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

onclick="alert('hello');return false;" does not work in IE7?

P: n/a
Hi,

Normally, a piece of code such as

<a href="http://www.yahoo.com" onclick="alert('hello');return
false;">link</a>

will stop the browser from actually going to href's destination.
However, this is not the case with the IE7 I am using. What has
changed?

IE7 version: 7.0.5730.11. Update versions: 0

Has anyone bumped into this problem before? How do I get around it?
Thanks.
Testcase:
-----------------------

<html>

<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');return
false;">my test link</a>
</body>
</html>

Dec 5 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
al******@gmail.com wrote:
Hi,

Normally, a piece of code such as

<a href="http://www.yahoo.com" onclick="alert('hello');return
false;">link</a>

will stop the browser from actually going to href's destination.
However, this is not the case with the IE7 I am using. What has
changed?
You have a newline after 'return', so javascript will insert a semicolon
before 'false'. Try taking out the newline and it works fine.

Dec 5 '06 #2

P: n/a

al******@gmail.com wrote:
Hi,

Normally, a piece of code such as

<a href="http://www.yahoo.com" onclick="alert('hello');return
false;">link</a>

will stop the browser from actually going to href's destination.
However, this is not the case with the IE7 I am using. What has
changed?
[...]
Testcase:
-----------------------

<html>

<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');return
false;">my test link</a>
</body>
</html>
The HTML is invalid, an A element must be inside a block element. I'm
amost certain that has nothing to do with it, particularly where IE is
concerned, but you never know! ;-)

--
Rob

Dec 5 '06 #3

P: n/a
RobG wrote:
al******@gmail.com wrote:
>Hi,

Normally, a piece of code such as

<a href="http://www.yahoo.com" onclick="alert('hello');return
false;">link</a>

will stop the browser from actually going to href's destination.
However, this is not the case with the IE7 I am using. What has
changed?
[...]
>Testcase:
-----------------------

<html>

<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');return
false;">my test link</a>
</body>
</html>

The HTML is invalid, an A element must be inside a block element. I'm
amost certain that has nothing to do with it, particularly where IE is
concerned, but you never know! ;-)
That only goes for XHTML. And since there's no doctype, IE will imply
HTML4, iirc. I'm with Duncan Booth elsewhere in this thread: the newline
is the problem.

--
Martijn Saly
Dec 5 '06 #4

P: n/a

Hi,

The newline is just a formatting problem with google. It does not
exist in the actual code.

In addition, I tried putting the linking inside <p></p>, but the
problem is still there.... other suggestions?

Thanks.
Martijn Saly wrote:
RobG wrote:
al******@gmail.com wrote:
Hi,

Normally, a piece of code such as

<a href="http://www.yahoo.com" onclick="alert('hello');return
false;">link</a>

will stop the browser from actually going to href's destination.
However, this is not the case with the IE7 I am using. What has
changed?
[...]
Testcase:
-----------------------

<html>

<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');return
false;">my test link</a>
</body>
</html>
The HTML is invalid, an A element must be inside a block element. I'm
amost certain that has nothing to do with it, particularly where IE is
concerned, but you never know! ;-)

That only goes for XHTML. And since there's no doctype, IE will imply
HTML4, iirc. I'm with Duncan Booth elsewhere in this thread: the newline
is the problem.

--
Martijn Saly
Dec 5 '06 #5

P: n/a
ASM
al******@gmail.com a écrit :
other suggestions?
Perhaps you have a tag 'object' or 'embed' or 'applet' in same page ?
if yes, it is a problem with your Windows !
I've heard the last update added something blocking popups.

M$ have then published a medicine
(something to break the protection they have just created ... ! ! ! !)

You'ld have to put after last <object ...>

<script type="text/javascript" src="ieupdate.js"></script>

and have this ieupdate.js in same folder.

file 'ieupdate.js' :
====================
objects = document.getElementsByTagName("object");
for (var i = 0; i < objects.length; i++)
{
objects[i].outerHTML = objects[i].outerHTML;
}

<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/activating_activex.asp>

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 5 '06 #6

P: n/a
VK

ASM wrote:
Perhaps you have a tag 'object' or 'embed' or 'applet' in same page ?
if yes, it is a problem with your Windows !
I've heard the last update added something blocking popups.

M$ have then published a medicine
(something to break the protection they have just created ... ! ! ! !)

You'ld have to put after last <object ...>

<script type="text/javascript" src="ieupdate.js"></script>

and have this ieupdate.js in same folder.

file 'ieupdate.js' :
====================
objects = document.getElementsByTagName("object");
for (var i = 0; i < objects.length; i++)
{
objects[i].outerHTML = objects[i].outerHTML;
}
That is the old Eolas workaround, nothing to do with links or popups.
Eolas' "no activation w/o interaction" block is not IE or Windows
specific: it is the same for any law-obeyant UA made over the last
year-and-half. btw this old workaround was contested as legally clean
because the object tag source code is still coming from the page.
document.write from external file is the current way (see my "Eolas
workaround" post)

Dec 5 '06 #7

P: n/a
VK
al******@gmail.com wrote:
Hi,

Normally, a piece of code such as

<a href="http://www.yahoo.com" onclick="alert('hello');return
false;">link</a>

will stop the browser from actually going to href's destination.
However, this is not the case with the IE7 I am using. What has
changed?
The security, I presume. That must be against the common trick "show
one URL in status bar, go to another one that mimics it". I guess you
may turn it off somewhere but you cannot expect it from your visitors.

If it is what I'm thinking it is, the psi-links of a kind
<a href="noscript.html" onclick="myFunction(); return false;">Select
all</ais another thing to say bye-bye sooner is better (the first one
is "holly hack")

Dec 5 '06 #8

P: n/a
al******@gmail.com wrote :
Hi,

Normally, a piece of code such as

<a href="http://www.yahoo.com" onclick="alert('hello');return
false;">link</a>

will stop the browser from actually going to href's destination.
However, this is not the case with the IE7 I am using. What has
changed?

IE7 version: 7.0.5730.11. Update versions: 0

Has anyone bumped into this problem before? How do I get around it?
Thanks.
Testcase:
-----------------------

<html>

<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');return
false;">my test link</a>
</body>
</html>
The problem, I think, is that IE7, at least for A tags, first tries to go
to href, and AFTER that, tries to execute onClick actions.
In fact :
<a href="javascript:null()" onClick="alert('clicked');"></a>
Doesn't work in IE, since null() stops JS.
On the other hand, that works:
<a href="#" onClick="alert('clicked');"></a>

I don't know why IE was made this way, since it is absolutely illogical
but I do not try to understand M$ anymore now...

Juste try to replace href="#" and then, test once again, and tell us :)

--
Naixn
http://fma-fr.net
Dec 5 '06 #9

P: n/a

I am not sure what is the problem, but this is the work around I am
using now:

<a href="javascript:;" onclick="foo(); return false;">link</a>

Not the best...
naixn wrote:
al******@gmail.com wrote :
Hi,

Normally, a piece of code such as

<a href="http://www.yahoo.com" onclick="alert('hello');return
false;">link</a>

will stop the browser from actually going to href's destination.
However, this is not the case with the IE7 I am using. What has
changed?

IE7 version: 7.0.5730.11. Update versions: 0

Has anyone bumped into this problem before? How do I get around it?
Thanks.
Testcase:
-----------------------

<html>

<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');return
false;">my test link</a>
</body>
</html>

The problem, I think, is that IE7, at least for A tags, first tries to go
to href, and AFTER that, tries to execute onClick actions.
In fact :
<a href="javascript:null()" onClick="alert('clicked');"></a>
Doesn't work in IE, since null() stops JS.
On the other hand, that works:
<a href="#" onClick="alert('clicked');"></a>

I don't know why IE was made this way, since it is absolutely illogical
but I do not try to understand M$ anymore now...

Juste try to replace href="#" and then, test once again, and tell us :)

--
Naixn
http://fma-fr.net
Dec 6 '06 #10

P: n/a
al******@gmail.com wrote:
Normally, a piece of code such as

<a href="http://www.yahoo.com" onclick="alert('hello');
return false;">link</a>

will stop the browser from actually going to href's
destination. However, this is not the case with the IE7
I am using. What has changed?

IE7 version: 7.0.5730.11. Update versions: 0
Trying exactly that code (inadvertent line wrapping removed), in exactly
that version of IE 7, reveals no issue. The navigation is cancelled as
expected.
Has anyone bumped into this problem before?
Lots of people fail to properly attribute cause and effect relationships
when scripting web browsers. An inability to post code that demonstrates
the issue if often a symptom of failing to properly attribute its cause
and effect relationships.

Where you say, "code such as" you probably mean code that consists of a
function call followed by a - return false; - statement. It has always
been the case that if an exception is thrown in the function call the
event handling function stops executing prior to the - return false; -
statement and so never returns any value at all to cancel the navigation
or not. Thus the navigation happens.
How do I get around it?
It is possible that some change in IE has started to cause the function
you call to start throwing exceptions and prevent your onclick handlers
ever getting to the - return false; - statement. It is identifying that
change that will facilitate the correct solution to your problem.
Testcase:
-----------------------

<html>

<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');
return false;">my test link</a>
</body>
</html>
If you are going to post a test case it is always a good idea to test it
yourself to verify that it does demonstrate what you expect it to
demonstrate. In practice this test case shows the alert and does not
navigate the browser (using IE7 version: 7.0.5730.11. on Windows XP
SP2).

Richard.
Dec 9 '06 #11

P: n/a
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote:
>Testcase:
-----------------------

<html>

<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');
return false;">my test link</a>
</body>
</html>

If you are going to post a test case it is always a good idea to test it
yourself to verify that it does demonstrate what you expect it to
demonstrate. In practice this test case shows the alert and does not
navigate the browser (using IE7 version: 7.0.5730.11. on Windows XP
SP2).
The test case *as the OP posted it* does show what he claimed. It shows the
alert and then navigates to the link (in any browser, not just IE 7).

Maybe the bad line-break wasn't his original problem, but it seems a touch
unfair to chastise him for posting a bad test case when in fact what he
posted did what he said until you corrected the line wrapping.
Dec 9 '06 #12

P: n/a
Duncan Booth wrote:
Richard Cornford wrote:
>>Testcase:
-----------------------
<html>
<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');
return false;">my test link</a>
</body>
</html>

If you are going to post a test case it is always a good
idea to test it yourself to verify that it does demonstrate
what you expect it to demonstrate. In practice this test case
shows the alert and does not navigate the browser (using IE7
version: 7.0.5730.11. on Windows XP SP2).

The test case *as the OP posted it* does show what he
claimed. It shows the alert and then navigates to the
link (in any browser, not just IE 7).
So it does not show any change between IE 6 and 7, and as the OP's
question took the form "What ahs changed?" (in part) its obviously
faulty formatting can be dismissed as ineptitude on the part of the
individual doing the posting.
Maybe the bad line-break wasn't his original problem, but it
seems a touch unfair to chastise him for posting a bad test
case when in fact what he posted did what he said until you
corrected the line wrapping.
But if he had tested what he had before he posted it he would not have
found that it did not exhibit the behaviour attributed to it, and
instead of having our time wasted with miss-attributed cause we could
have been looking at something that really has changed between IE 6 and
7 and maybe learnt something of ongoing practical use (though more
likely seen that an exploitable bug in IE 6 had finally been fixed).

Richard.
Dec 10 '06 #13

P: n/a
RobG wrote:
al******@gmail.com wrote:
[...]
>Testcase:
-----------------------

<html>

<body>
<phello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');return
false;">my test link</a>
</body>
</html>

The HTML is invalid, an A element must be inside a block element.
IBTD.
I'm amost certain that has nothing to do with it, particularly where IE
is concerned, but you never know! ;-)
When there is no DOCTYPE declaration in the markup, or one that can be
deduced from additional facts in the posting (like VK stating Validator
results), it makes little sense to point out possible invalid snippets,
provided that there is an active (X)HTML standard that can be referred to
where it can still be Valid.

For example, if the above HTML code was declared HTML 4.01 Transitional (as
it would be considered by a recent UA implementing a Quirks Mode), would
still be not pretty, but perfectly Valid.
Regards,

PointedEars
--
The German psychs, the German authorities, the German secret service agents
are [...] fanatics, they are insane and known of persecuting innocent people
and Scientologists. -- "The only real Barbara Schwarz", dsw.scientology,
<16**************************@posting.google.com >
Dec 11 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.