Tuesday, January 13, 2004

Disable Printing Of A Web Page

And still another trick I have learned while working on my current ASP.NET project is how to prevent a user from printing the web page that they are viewing. This is a two step process. The first step is very simple. Add this tag in the HEAD section of your page:

<style media="print">
BODY { DISPLAY: none }
</style>


---

What this stylesheet entry does is tell the browser to replace everything in between the BODY tags of the page with nothing when the user tries to print the page. All the user will get is a blank page.

However, nothing prevents a user from saving the page to his hard drive and editing the style tag out of the page. So much for that security. JavaScript to the rescue! Add this code to HEAD of the page:


// Prevent selecting text for copying.
function deselect(){
if (document.selection) {
document.selection.empty();
Copied=document.body.createTextRange();
}
else if (window.getSelection){window.getSelection().removeAllRanges();}}

timerID=setTimeout('setInterval("deselect()", 1)',1);

// Capture right-click and prevent it to prevent copying text or viewing source.
function clickIE4(){
if (event.button==2){
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all){
document.onmousedown=clickIE4;
}

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



---

This code prevents right-clicking and prevents selecting of text in Netscape (and IE). To make absolutely sure that nobody can copy text from the page in IE, add this code to the BODY tag:

<BODY onselectstart="return false;" ondragstart="return false;">


---

That will prevent any selecting of text at all (but it is not supported in Netscape, so you'll need the previous code for that).

Of course, this still does not prevent the user from doing a File->Save on your page. To prevent that, just use javascript to open the page that should not be printed in a new window without the toolbars. This also has the added security of preventing the user from disabling javascript as a trick to get the page contents. If javascript is disabled in the browser, the page will not come up since javascript is used to launch the window.

The final possibility is that the user could enable javascript, open the window, disable javascript while the window is open and then hit F5 to refresh the opened page. With the javascript disabled, the user would be able to copy the page contents.

Now, you can trap the refresh key with javascript, but you cannot prevent refreshing the page. All you can do is preform some task before the page is refreshed. In order to be totally secure, you can have the window close if the user tries to refresh the page. Here's the code (goes in the HEAD tag):


function preventRefresh(e)
{
// Netscape uses e, IE window.event
var eventObj = (e)?e:window.event;

if(eventObj.which)
{
//NS4+
if(e.which==116)
{
alert('No refreshing of this page is allowed.' +
'This window will now close.');
window.close();
}
} else {
//IE
if(window.event.keyCode==116)
{
alert('No refreshing of this page is allowed.' +
'This window will now close.');
window.close();
}
}
}
if(document.layers)
{
//NS4+
document.captureEvents(Event.ONKEYDOWN);
}
document.onkeydown=preventRefresh;



---

Voila! An unprintable, uncopyable secured web page.