2019/05/26
javascript JQuery
備忘録です。
参考サイト
テキストのドラッグ、画像のコピーを禁止して、記事をコピーさせない方法
html bodyに記述
<body oncontextmenu="return false;">
javascriptを記述
<script>
document.onselectstart = function(){ return false; };
document.onmousedown = function(){ return false; };
document.body.onselectstart = "return false;"
document.body.onmousedown = "return false;"
</script>
右クリック禁止と併用することで2重にプロテクトできます。
しかしながら実装したところselectが使えなくなりました。
onmousedownを無効にすることによりselectのダウン表示まで無効になってしまうのが原因でした。
そこで画像だけドラッグできない方法を見つけました。
$(function(){
$('img').attr('onmousedown', 'return false');
$('img').attr('onselectstart', 'return false');
$('img').attr('oncontextmenu', 'return false');
});
透過gifを画像の上にのせることによりコピーを防ぐ方法です。
1 x 1pxのblank.gifを作成します。
下記をコピーしてdwImageProtector.jsを作成
<script>
jQuery.fn.protectImage = function(settings) {
$("img[name=blank]").remove();
settings = jQuery.extend({
image: 'blank.gif', //パスを指定してください
zIndex: 10
}, settings);
return this.each(function() {
var position = $(this).position();
var height = $(this).height();
var width = $(this).width();
$('<img />').attr({
width: width,
height: height,
src: settings.image,
name : "blank"
}).css({
top: position.top,
left: position.left,
position: 'absolute',
zIndex: settings.zIndex
}).appendTo('body')
});
};
</script>
<head>~</head>内に記述
<script type="text/javascript" src="dwImageProtector.js"></script>
<script type="text/javascript">
$(window).bind('load', function() {
$('img').protectImage();
});
$(window).bind('resize', function() {
$('img.protect').protectImage();
});
</script>
適用する画像にclassを付けます。
<img class="protect">
すべての画像に適用する場合は下記のようにします。
$('img').protectImage();
この方法だと画像をドラッグしてもblank.gifが保存されます。
2026/11/19
ホームページ制作
2026/11/18
Mac
Linux
2026/11/17
Claude Code
AI
2026/11/16
Wordpress
プラグイン
WordPressのログイン対策、入れっぱなしで大丈夫?reCAPTCHAの乗り換えと締め出しの備忘録から分かった5つのこと
2026/11/15
バイク