Click to See Complete Forum and Search --> : capturing NS7 mousedown events


treker79
03-07-2003, 03:04 PM
I am attempting to discourage surfers from copying images on my site by displaying an alert. The version of the code I currently use works in everything except NS7.

This code below (from The JavaScript Source: Page Details: No Right Click) also does not seem to work in NS7... is there something I'm missing? Is there a way to do what I'm looking to do? I have a feeling it's something simple...

Thanks in advance for your help!

Ed
=================

<HEAD>

<SCRIPT LANGUAGE="JavaScript1.1">
<!-- Original: Martin Webb (martin@irt.org) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function right(e) {
if (navigator.appName == 'Netscape' &&
(e.which == 3 || e.which == 2))
return false;
else if (navigator.appName == 'Microsoft Internet Explorer' &&
(event.button == 2 || event.button == 3)) {
alert("Sorry, you do not have permission to right click.");
return false;
}
return true;
}

document.onmousedown=right;
document.onmouseup=right;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
if (document.layers) window.captureEvents(Event.MOUSEUP);
window.onmousedown=right;
window.onmouseup=right;
// End -->
</script>
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<center>[ Try right-clicking the image and then the text link below ]
<p>
<img src="http://javascript.internet.com/img/tjsbutton.gif">
<p>
<a href="http://javascript.internet.com/page-details/no-right-click.html">No Right Click Link</a>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size: 1.57 KB -->

=================

For your reference, here is the code as I currently use it:

=================

<Script language="JavaScript">

<!-- NoScript
// Present a notice asking the user not to save the image
function NoSave() {
date = new Date();
year = date.getYear();
if (year < 1900) {
year += 1900;
}
mesg = "This image is Copyright " + year + "Portrait & Wedding Gallery\n";
mesg += "All Rights Reserved.\n";
alert(mesg);
return false;
}
// NoScript -->

</Script>

<Script language="JavaScript1.2">

<!-- NoScript
// Present a notice asking the user not to save the image
function NoSave(e) {
if (navigator.appName == "Netscape") {
if (e.target.name == "ViewedImage" && e.which != 1) {
date = new Date();
year = date.getYear();
if (year < 1900) {
year += 1900;
}
mesg = "This image is Copyright " + year + "Portrait & Wedding Gallery\n";
alert(mesg);
return false;
}
return true;
} else {
date = new Date();
year = date.getYear();
if (year < 1900) {
year += 1900;
}
mesg = "This image is Copyright " + year + "Portrait & Wedding Gallery\n";
alert(mesg);
return false;
}
}

// Capture mousedown events
if (navigator.appName == "Netscape") {
document.captureEvents(Event.MOUSEDOWN);
document.onMouseDown = NoSave;
}
// NoScript -->

</Script>

<img src="sunset.jpg" onmousedown="NoSave()" name="ViewedImage" width="352" height="265" border="0" alt="sunset">

khalidali63
03-07-2003, 04:43 PM
Look at the lines below in your code


if (document.layers) window.captureEvents(Event.MOUSEDOWN);
if (document.layers) window.captureEvents(Event.MOUSEUP);


you are only capturing events for N<5

this will work for all

if (!document.all && (document.layers || document.getElementByID) ) {
window.captureEvents(Event.MOUSEDOWN);
window.captureEvents(Event.MOUSEUP);
}

Cheers

Khalid

treker79
03-11-2003, 01:23 AM
Thanks for your help. It pointed me in the right direction. After a bit of testing, I ended up with this:


<Script language="JavaScript">

<!-- NoScript
// Present a notice asking the user not to save the image
date = new Date();
year = date.getYear();
if (year < 1900) { year += 1900; }
mesg = "This image is Copyright " + year + " All Rights Reserved.\n";

function NoSave(e) {
if (navigator.appName == "Netscape") {
if (e.target.name == "ViewedImage" && e.which != 1) {
alert(mesg);
return false;
}
return true;
} else {
alert(mesg);
return false;
}
}

// Capture mousedown events
if (navigator.appName == "Netscape") {
document.captureEvents(Event.MOUSEDOWN);
document.onMouseDown = NoSave;
}

document.oncontextmenu=new Function("return false")

// NoScript -->

</Script>

<a onmousedown='alert(mesg)'><img src="sunset.jpg" name="ViewedImage" width="352" height="265" border="0" alt="sunset"></a>


=================

This will allow me to specify which images display the alert, prevents the context menu from displaying, and works in NS4.77, IE6, and NS7. (I believe it may even work on a MAC, but I don't have one to test with...)

Thanks again.
Ed