Stopping ctl-click in IE

D

Dave

I have a situation where I want to react to a ctrl-click on a <span> and
it works in Netscape and Firefox browsers but in IE I have a problem.
In IE I do catch the ctrl-click but IE also renders the span in inverse
video, essentially selecting the item.

Here is a short sample that demonstrates the issue:

<html><head>
<script type="text/javascript">

function Clicked(evt){
evt.cancelBubble=true;
}

</script></head>
<body>
<SPAN onClick="Clicked(event);">click me</SPAN>
</body></html>

I thought the cancelBubble would prevent the event from triggering the
selection from happening but I think that the ctrl-click selection
happens before I get control.

Is there a way to prevent the selection from being rendered on ctrl-
click while still allowing my javascript to react to the event?

browser versions used
ie 6.0.2800
firefox 0.9
netscape 7.1
 
R

RobB

Dave said:
I have a situation where I want to react to a ctrl-click on a <span> and
it works in Netscape and Firefox browsers but in IE I have a problem.
In IE I do catch the ctrl-click but IE also renders the span in inverse
video, essentially selecting the item.

Here is a short sample that demonstrates the issue:

<html><head>
<script type="text/javascript">

function Clicked(evt){
evt.cancelBubble=true;
}

</script></head>
<body>
<SPAN onClick="Clicked(event);">click me</SPAN>
</body></html>

I thought the cancelBubble would prevent the event from triggering the
selection from happening but I think that the ctrl-click selection
happens before I get control.

Is there a way to prevent the selection from being rendered on ctrl-
click while still allowing my javascript to react to the event?

browser versions used
ie 6.0.2800
firefox 0.9
netscape 7.1

Strictly MSIE:

<span onselectstart="return false;">...eeeeeeesh!!</span>
 
L

Lasse Reichstein Nielsen

Dave said:
<script type="text/javascript">

function Clicked(evt){
evt.cancelBubble=true;
}

</script></head>
<body>
<SPAN onClick="Clicked(event);">click me</SPAN>
</body></html>

I thought the cancelBubble would prevent the event from triggering the
selection from happening but I think that the ctrl-click selection
happens before I get control.

Assigning true to "event.cancelBubble" is the IE way of stopping further
event handlers in the page, not stopping the default behavior of the
click. To do that, assign false to "event.returnValue".

A general function that works for standards compliant browsers as well:
---
function clicked(evt){
if (evt.preventDefault) {
evt.preventDefault(); // The W3C DOM way
} else {
evt.returnValue = false; // The IE way
}
}
 
D

Dave

A general function that works for standards compliant browsers as well:
---
function clicked(evt){
if (evt.preventDefault) {
evt.preventDefault(); // The W3C DOM way
} else {
evt.returnValue = false; // The IE way
}
}
---

Thanks for the response. It didn't work for me. I included the updated
example below.

I noticed one thing though. If I put an alert immediately before the
evt.returnValue = false; I see that by the time the alert box is
visible, "click me" is already highlighted. So I'm not sure how that
event property could have an effect on the outcome.


<html><head>
<script type="text/javascript">

function Clicked(evt){
if (evt.preventDefault) {
evt.preventDefault(); // The W3C DOM way
} else {
evt.returnValue = false; // The IE way
}
evt.cancelBubble=true;
}

</script></head>
<body>
<SPAN onClick="Clicked(event);">click me</SPAN>
</body></html>
 
D

DU

Lasse said:
---
function clicked(evt){
if (evt.preventDefault) {
evt.preventDefault(); // The W3C DOM way
} else {

Shouldn't that be else if(window.event)
{event.returnValue = false;};
evt.returnValue = false; // The IE way
}
}

DU
 
L

Lasse Reichstein Nielsen

DU said:
Shouldn't that be else if(window.event)
{event.returnValue = false;};

In this scenario, the function was called from an HTML event
handler:
<blah onblah="clicked(event); .. ">
so "evt" does refer to the global event handler in IE too.

It should probably be:

<blah onblah="return clicked(event);">
....
function clicked(evt) {
...
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false; // can't hurt
return false;
}
}

.... to handler thr likes of Netscape 4.
/L
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top