Avatar of peterchamberlin
peterchamberlin

asked on 

IE7 a href + onclick - both happen!

I have an annoying behaviour in IE7. With an HTML page containing the following script...

<p>
<a href="http://news.bbc.co.uk/" onclick="window.open('http://news.bbc.co.uk/', 'popup'); return false">Click me</a>
</p>

...if I click the link then I get a popup window, but the launching page also navigates to the href location behind it. Seemingly the href happens before the onclick event, or the onclick return value is not taken into consideration properly.

What's strange is that on other XP PCs here with IE7 it works okay, but on my work machine and home machine it doesn't. My suspicion is that VS 2005 has had some impact. I'm on VS2005 SP1 here, and original version at home.

I have reset all of IE7's settings to try to get around this problem but to no avail. I have also uninstalled the IE7 Beta3 developer toolbar. Even running in no-addon mode has no effect. Both machines are fully-patched XP SP2. I have also installed IE7 atop itself, installed M$ Script 5.6, copied registry settings from HKLM/HKCU Internet Explorer and Internet Settings from another correctly behaving XP SP2 / IE7 machine. I've tried logging on as local machine admin for a clean HKCU, nothing has fixed this.

I found some people claiming this same issue on newsgroups, others trying similar example code and getting the correct result, but no explanation in either case as to what was wrong.

This issue only affects IE7, not IE6 or Mozilla Firefox.

Thoughts, suggestions or solutions all welcome!
Web BrowsersWeb Languages and StandardsJavaScript

Avatar of undefined
Last Comment
Computer101
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Very strange. The return false on the onClick should stop the navigation unless you have an error in the code before it. Are you sure that the above code as pasted will give you this situation or is it simplified for this question?
Avatar of raj3060
raj3060
Flag of United States of America image

Try this:

<p>
<a href="#" onclick="window.open('http://news.bbc.co.uk/', 'popup'); return false">Click me</a>
</p>
Avatar of raj3060
raj3060
Flag of United States of America image

if above didn't work this should work:

<script>
function openWin(){
var win1 = window.open('http://news.bbc.co.uk/', 'popup');
}
</script>
</head>
<body>
<p>
<a href="javaScript:{openWin()}">Click me</a>
</p>
Raj: The first suggestion will reload the page and the second will unload anything running in the page too...
At least use void:
<a href="javaScript:void(openWin())">Click me</a>
Avatar of raj3060
raj3060
Flag of United States of America image

Thanks Mike.
Wrote in hurry.
Avatar of peterchamberlin
peterchamberlin

ASKER

I'm not looking for alternatives here, I want a reason and solution as to why IE7 isn't behaving as it should, and only on my home and work PC.

The change of href to # will of course only open the popup window and the launch page navigates to #, so seems not to have changed but the request was still made.

I created the simplified sample for test purposes, but the original problem was noted with the BBC website where media player and pictures open in popup windows while opening in the launch page too. Both of them exhibit the same behaviour.
Avatar of raj3060
raj3060
Flag of United States of America image

Code you presented would behave like you mentioned based on the connection, content loading etc. You are telling your link to go to a different page and then you are telling it to load similar contents in a popup and then when popup loads then you are returning false to not to load the contents in same window. It would work fine if the order of the operation goes as you are predicting. But order is messing up due to content loading in popup, meanwhile your link is already loaded.
Remember onclick event is firing two things in your code at the same time but probably not in the same order and even if the order is how it looks then content loading is probably messing up the return code.

You can do this by using js function where you can order your events.

I hope it helps.
Avatar of peterchamberlin

ASKER

In IE6, and Firefox, the onclick event gets priority over the A HREF click. By returning false it indicates that the standard click behaviour should be cancelled. Both these browsers have consistently worked correctly with this. In IE7 it's permanently in the wrong order, not time specific. Please note that I don't have access to the content causing this issue, since links like this are used all over the place on sites such as news.bbc.co.uk etc. I'm trying to resolve the browser behaviour peculiar to my home and office machines. Installing IE7 on to an XP SP2 image responds to that script as you'd expect, i.e. only opening the pop-up window.

I imagine this has to be a security restriction somewhere (I have popup blocker disabled for these tests by the way), or an event model issue with IE / security patches / JScript implementation.
Avatar of peterchamberlin

ASKER

I noticed that if I set event.returnValue = false in the script then IE7 works correctly. It seems to be a breakage between "return false" and IE setting that value itself. Thus:

<html>
<body>
<p>
<a href="http://news.bbc.co.uk/" onclick="window.open('http://news.bbc.co.uk/', 'popup'); event.returnValue = false; return false">Click me</a>
</p>
</body>
</html>
Thanks for sharing
Avatar of peterchamberlin

ASKER

Hoorah! I have found the solution to this niggling problem!
It was all down to having upgraded FlashGet to one of the newer versions.
Even though I was running IE in no-addon mode the FlashGet browser hooks were still interacting with IE causing the JavaScript return value of false to be lost.
Uninstalling FlashGet and rebooting cured this issue.

I will contact the author of FlashGet to alert him to this problem.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo