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

Stopping ctl-click in IE

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Dave wrote:
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>

Jul 23 '05 #2

P: n/a
Dave <noemail@anyaddressiown_invalid.com> writes:
<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
}
}
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #3

P: n/a
In article <oe**********@hotpop.com>, lr*@hotpop.com says...

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>
Jul 23 '05 #4

P: n/a
In article <11*********************@g14g2000cwa.googlegroups. com>,
fe******@hotmail.com says...
Strictly MSIE:

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


That's too bad it's a MSIE specific solution, but it did work.

Thanks!
Jul 23 '05 #5

P: n/a
DU
Lasse Reichstein Nielsen wrote:
---
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
--
The site said to use Internet Explorer 5 or better... so I switched to
Mozilla 1.7.6 :)
Jul 23 '05 #6

P: n/a
DU <dr*******@hotNOSPAMmail.com> writes:
Lasse Reichstein Nielsen wrote:
---
function clicked(evt){ if (evt.preventDefault) {
evt.preventDefault(); // The W3C DOM way
} else {


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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.