I need to put an image in my page. I want to disable dragging of that image. I am trying lot of things but no help. Can somebody help me out ?
I don't want to keep that image as a background-image because I am resizing the image.
I need to put an image in my page. I want to disable dragging of that image. I am trying lot of things but no help. Can somebody help me out ?
I don't want to keep that image as a background-image because I am resizing the image.
You can like this...
document.getElementById('my-image').ondragstart = function() { return false; };
See it working (or not working, rather)
It seems you are using jQuery.
$('img').on('dragstart', function(event) { event.preventDefault(); });
simplest cross browser solution is
<img draggable="false" ondragstart="return false;" src="..." />
problem with
img {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-user-drag: none;
user-drag: none;
-webkit-touch-callout: none;
}
is that it is not working in firefox
img {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-user-drag: none;
user-drag: none;
-webkit-touch-callout: none;
}
I used it on my website at http://www.namdevmatrimony.in/ It's works like a magic!!! :)
See this answer; in Chrome and Safari you can use the following style to disable the default dragging:
-webkit-user-drag: auto | element | none;
You could try user-select for Firefox and IE(10+):
-moz-user-select: none | text | all | element
-ms-user-select: none | text | all | element
You can use inline code for this
<img draggable="false" src="http://www.ourkanpur.com/images/logo.png">
And the second option is use external or on-page css
img {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-user-drag: none;
user-drag: none;
-webkit-touch-callout: none;
}
<img src="http://www.ourkanpur.com/images/logo.png">
Both are Working Correctly I m using external css on this site (Click Here)
Directly use this: ondragstart="return false;"
in your image tag.
<img src="http://image-example.png" ondragstart="return false;"/>
If you have multiple images, wrapped on a <div>
tag:
<div ondragstart="return false;">
<img src="image1.png"/>
<img scr="image2.png"/>
</div>
Works in all major browsers.
Since my images were created using ajax, and therefore not available on windows.load.
$("#page").delegate('img', 'dragstart', function (event) { event.preventDefault(); });
This way I can control which section blocks the behavior, it only uses one event binding and it works for future ajax created images without having to do anything.
With jQuery new on
binding:
$('#page').on('dragstart', 'img', function(event) { event.preventDefault(); });
(thanks @ialphan)
Well I don't know if the answers in here have helped everyone or not, but here's a simple inline CSS trick which would definitely help you to disable dragging and selecting texts on a HTML page.
On your <body>
tag add ondragstart="return false"
. This will disable dragging of images. But if you also want to disable text selection then add onselectstart="return false"
.
The code will look like this: <body ondragstart="return false" onselectstart="return false">
You may consider my solution the best. Most of answers are not compatible with old browsers like IE8 since e.preventDefault() wont be supported as well as ondragstart event. To do it cross browser compatible you have to block mousemove event for this image. See example below:
jQuery
$("#my_image").mousemove( function(e) { return false } ); // fix for IE
$("#my_image").attr("draggable", false); // disable dragging from attribute
without jQuery
var my_image = document.getElementById("my_image");
my_image.setAttribute("draggable", false);
if (my_image.addEventListener) {
my_image.addEventListener("mousemove", function(e) { return false });
} else if (my_image.attachEvent) {
my_image.attachEvent("onmousemove", function(e) { return false });
}
tested and worked even for IE8
Well, this is possible, and the other answers posted are perfectly valid, but you could take a brute force approach and prevent the default behavior of mousedown
on images. Which, is to start dragging the image.
Something like this:
window.onload = function () {
var images = document.getElementsByTagName('img');
for (var i = 0; img = images[i++];) {
img.ondragstart = function() { return false; };
}
};
document.getElementById('#yourImageId').addEventListener('dragstart', function(e) {
e.preventDefault();
});
Works in this Plunker http://plnkr.co/edit/HbAbJkF0PVLIMjTmEZml
Stealing from my own answer, just add a completely transparent element of the same size over the image. When you resize your image, be sure to resize the element.
This should work for most browsers, even older ones.