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

A

alvin.yk

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>
<p> hello1 </p>
<a href = 'http://www.yahoo.com' onclick="alert('clicked');return
false;">my test link</a>
</body>
</html>
 
R

RobG

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>
<p> hello1 </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! ;-)
 
M

Martijn Saly

RobG said:
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>
<p> hello1 </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.
 
A

alvin.yk

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 said:
RobG said:
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>
<p> hello1 </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.
 
A

ASM

(e-mail address removed) 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.outerHTML = objects.outerHTML;
}

<http://msdn.microsoft.com/library/d.../author/dhtml/overview/activating_activex.asp>
 
V

VK

ASM said:
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.outerHTML = objects.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)
 
V

VK

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</a> is another thing to say bye-bye sooner is better (the first one
is "holly hack")
 
N

naixn

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>
<p> hello1 </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 :)
 
A

alvin.yk

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

Richard Cornford

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>
<p> hello1 </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.
 
R

Richard Cornford

Duncan said:
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.
 
T

Thomas 'PointedEars' Lahn

RobG said:
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
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top