IE 7 CTRL + click opens a new window - how to suppress it?
Asked Answered
F

6

3

Is it possible to suppress the default IE 7 functionality when CTRL+click on link opens a new window? if so, how?

Thanks!

Flyfish answered 22/4, 2010 at 20:55 Comment(3)
All I'll say is that you need a very good reason for redefining actions that users' expect to work in a certain way.Austreng
It does in IE8; this is also the default behavior in Firefox and Chrome - shift-click opens a new window, ctrl-click opens a new tab. Probably the same in others too.Impatient
You need to be more detailed in your request. Your actual problem is much more scoped than this.Pileate
R
7

There is no way to suppress a Ctrl + Click on a link with no child elements in Internet Explorer -- the onclick event doesn't fire at all for link clicks if the Ctrl key is held down. It seems that Microsoft don't want you to change this functionality out of fear that you might confuse the user.

I searched for some sort of official confirmation/explanation before posting this answer, but unfortunately this limitation is not listed in the documentation on MSDN and Google wasn't helpful. Nevertheless, it remains true, try it yourself:

<a href="#" onclick="alert('Hello');">Hello</a>

You will find that a Ctrl + click on the link will not throw the alert box. According to pinkgothic, assigning a child element to the link will work around the problem. For example:

<a href="#" onclick="alert('Hello');"><span>Hello</span></a>

This works because the click is triggered for the <span> element first, before propagating to the <a> element.

Rhyton answered 22/4, 2010 at 21:24 Comment(5)
yeah i figured already....i will go with the out of the box behaviour for the tree control and call it a dayFlyfish
Interestingly enough, <a href="http://stackoverflow.com" onclick="alert('Hello');"><span>Hello</span></a> with a ctrl+click works fine (at least in IE9). Apparently it 'only' matters where the event bubbles up from. A <span> is enough to throw it off.Reive
@pinkgothic: That sounds promising if it works in older versions of IE too.Rhyton
Agreed! Unfortunately I have no means to check that; I just stumbled across your answer while trying to debug a site that was mysteriously not working with ctrl+click in IE :) and I managed to get it fixed (our case was a simple display:none triggered onclick), so I figured I'd share my fix. Your answer was a great help! ^_^Reive
The trick with the <span> element suggested by @Reive works great. Thanks for this!Zuleika
S
1

The jQuery event.preventDefault() method or similar can override default behavior on pages that you have control over.

It is generally bad practice to alter the behaviour of a user's browser without really good reason as the browser and its behaviour is "their's".

Suffragist answered 22/4, 2010 at 20:59 Comment(1)
i am planning to do in for nodes in a tree. they are supp. to be clickable but not open links. I am not going to do this for traditional links.Flyfish
O
1

had the same problem as the op and solved it by giving the anchor href attribute an '#' and an additional data-href attribute with the corresponding link location. The downside you need a click handler to follow the link, also right clicking "open in new window" wont work with this approach.

eg: In AnchorTag, Use href and data-ref as:
<a id="logout" href="#" data-href="${yourPath}"> 

And in javascript, use    
$("#logout").click(function(e) {
    if(e.ctrlKey) 
e.preventDefault();
window.location = $(e.target).attr("data-href");
});
Olathe answered 21/10, 2011 at 12:54 Comment(0)
G
1

wrap your link text inside an span. like this:

<a href="test.html"><span>click here!</span></a>
Georgiana answered 4/2, 2014 at 10:1 Comment(0)
M
0

Add script to the anchor and return false

<a href='some.htm' onclick='return false;'></a>

And it is valid to use anchors in a treeview because it makes the treeview more accessible.

Mielke answered 22/4, 2010 at 21:16 Comment(0)
I
0

Using jquery you can handle the ctrl+click functionality for yourself and return false, for example

$("a").click(function(event){
if(event.metaKey || event.ctrlKey){
 //handle over here 

return false;
}

});

this works across all browsers including mac

Inquisition answered 16/5, 2011 at 5:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.