1

I have <div>Some text</div>, and i want to make it unclickable (i want elements that under that div to be selected instead this div), unselectable (so user couldn't select text inside this div), but visible... is it possible for IE6 + IE7 + IE8 + IE9?

Update

I just want to render some text on top of picture, but i want picture to be the only one who can catch mouse events.. so i want text to be rendered, but not involved in mouse events at all..

obenjiro
  • 3,665
  • 7
  • 44
  • 82
  • You want to prevent users from copying your content? Nope. –  Apr 03 '11 at 19:26
  • @Ai Is that DIV absolutely positioned? – Šime Vidas Apr 03 '11 at 19:27
  • Not sreal, i just want to render some text on top of picture, but want picture to be the only one who can catch mouse events.. so i want text to be rendered, but not involved in mouse events.. – obenjiro Apr 03 '11 at 19:28
  • @sime-vidas well.. yes.. – obenjiro Apr 03 '11 at 19:53
  • If you have the text as a child of the image div, absolutely positioned over the top, then the events will propagate up to the image div if you do not catch them. The [text selection can be blocked via CSS](http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting). – Orbling Apr 03 '11 at 21:26
  • Can you provide some example HTML/CSS which includes this picture? – Richard JP Le Guen Apr 07 '11 at 00:54
  • @Richard You can find example at @Rodin answer... – obenjiro Apr 07 '11 at 10:46

6 Answers6

3

Try overlaying the image and text with another div (named capturebox in my example) and capture mouse events on that.

In order for capturebox to really capture events in IE, it must have a background color set. To make it transparent, I give it an opacity of 0:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script>
            function captureclick(event) {
                alert('capurebox');
            }
        </script>
        <style>
            .imgbox {
                position: absolute;
                top: 0px;
                left: 0px;
            }

            .imgbox img {
                width: 200px;
                height: 200px;
            }

            .imgbox p {
                cursor: default;
                position: absolute;
                top: 50px;
                left: 50px;
            }

            .capturebox {
                filter: alpha(opacity=0);
                background-color: white;
                height: 200px;
                width: 200px;
                position: absolute;
                left: 0px;
                right: 0px;
            }

        </style>
    </head>
    <body>
        <div class="imgbox">
            <img src="yourimage.jpg"/>
            <p>Some text</p>
            <div class="capturebox" onclick="captureclick(event)"></div>
        </div>
    </body>
</html>
Marijn van Vliet
  • 5,239
  • 2
  • 33
  • 45
  • Cool.. but this solution dosen't work in IE6... `filter: alpha(opacity=0);` isn't working in IE6 – obenjiro Apr 04 '11 at 07:12
  • True. I wonder if it's possible at all in IE6. Take a look at this guide for inspiration: http://www.quirksmode.org/js/events_order.html – Marijn van Vliet Apr 04 '11 at 09:42
  • His example doesn't have a doctype, making it an awful example. http://www.w3.org/QA/2002/04/valid-dtd-list.html – reisio Apr 07 '11 at 16:13
  • @reisio I would not say excluding a doctype makes it an 'awful' example. Incomplete, yes, but awful? – peteorpeter Apr 07 '11 at 18:17
  • @Rodin - I was looking at a similar solution. I found that the user could still select the text by selecting the area surrounding the image, text and capturebox... Not sure if there's a way around that. Otherwise, I think this kind of technique is more elegant than using canvas and requiring a VML shim for IE. – peteorpeter Apr 07 '11 at 18:20
  • @peteorpeter: yes, awful. It's actually worse than no example at all. – reisio Apr 08 '11 at 06:20
  • @reisio: I'm terribly sorry for omitting the docstring. I was being lazy. The answer is now updated for optimal XHTML compliance. Thank you for pointing it out. – Marijn van Vliet Apr 08 '11 at 11:36
  • @peteorpeter: I don't think being able to select the text is a problem in this case. It was not explicitly stated in the question that it should not be allowed, just that the default case would have to be to ignore mouse events on the text. – Marijn van Vliet Apr 08 '11 at 11:38
  • @Rodin: Yep, I was thinking "mouse events" would include selection, but OP updated to clarify that. @reisio: Well, I admire your attention to detail! – peteorpeter Apr 08 '11 at 13:36
3

You can disable your text by setting the unselectable attribute <p unselectable="on"> and setting the CSS property -moz-user-select: none;

Assuming your text is inside a <p> you can trigger a click to the image when p is clicked on.

$("p").click(function() {
    $(this).parent().find('img').trigger('click')
});

$('img').click(function() {
    alert('image clicked');
})

Check working example at http://jsfiddle.net/pavgY/1/

Hussein
  • 42,480
  • 25
  • 113
  • 143
  • WOW :) it's really working everyware!! IE6, IE7, IE8, IE9.. tnx man... you saved my day.. – obenjiro Apr 08 '11 at 08:53
  • 1
    +1 Very slick! Also, if you want to make sure your HTML validates (pretty sure `unselectable` isn't in spec), you could add that for IE in JS. – peteorpeter Apr 08 '11 at 13:42
  • Hmm. I guess a lot can happen in a year. That fiddle doesn't work for me in IE8 or in Chrome... – Chris Jaynes May 11 '12 at 20:02
2

Here's a working example (at least in Chrome and IE6, can't speak for IE7-9) using Raphael to render the text on top, and a little jQuery to route events appropriately.

gotta love marsupials!

(Gotta love marsupials!)

I was surprised to find that the click event pass-through worked in VML in IE6! Also, the VML is not selectable by default, which in this case is nice.

The starting markup is just <img alt="the text you want to show" />, so it's a pure JS enhancement.

This is basically the SVG-based equivalent of the canvas-based solution proposed by @Eldar.

peteorpeter
  • 4,037
  • 2
  • 29
  • 47
  • It works perfectly for IE9, but that's all, click event dosen't work in IE7-IE8, in IE6 there is no text at all... http://cid-a1de71e9f2ae2f82.office.live.com/self.aspx/.Public/noname.png – obenjiro Apr 08 '11 at 08:44
  • Hmmm... My IE6 VM shows the text fine, and I did notice the IE7-8 click event trouble. Oh well, I'm not going to dig any further - it was a heavy-handed solution to begin with! @Hussein's approach is light years simpler. At least I got to post a picture of a wombat. – peteorpeter Apr 08 '11 at 13:50
1
  var element = document.getElementById('content');
  element.onselectstart = function () { return false; } // ie
  element.onmousedown = function () { return false; } // mozilla

Try this

Headshota
  • 21,021
  • 11
  • 61
  • 82
  • Text is not selecting, but this div still "capture" mouse events, so if i click on that div, element that under do not invoke onmousedown event.. – obenjiro Apr 03 '11 at 19:31
1

Could you just place a clear image on top?

Freesnöw
  • 30,619
  • 30
  • 89
  • 138
  • nope.. text that i render on top of that image.. is varible.. so i nead an acsess to content of that div, but i what that div to be unclickable.. – obenjiro Apr 04 '11 at 06:34
1

You can use canvas to put text ontop of image. Sure HTML5 is not working for IE6 but you can use http://code.google.com/p/explorercanvas/ googles library to emulate it there.

Eldar Djafarov
  • 23,327
  • 2
  • 33
  • 27
  • this library use Silverlight... no sorry, i want to use pure Javascript, CSS, HTML.. – obenjiro Apr 07 '11 at 10:45
  • too bad, I guess there are no good crossbrowser solution:( The text will anyway catch all events. You just have to deal with them. – Eldar Djafarov Apr 07 '11 at 10:54
  • The only problem is that Users that use IE6 will never install something like Silverlight or VML.. well.. at least the ones that i know :) – obenjiro Apr 07 '11 at 11:34