Solved

JavaScript - How do I disable printing my webpage?

Posted on 2011-02-21
14
285 Views
Last Modified: 2012-05-11
Hi,

I want users not to be able to print my page, not even ctrl A to select all then ctrl C to copy, cuz they can paste it later in Word and print!
0
Comment
Question by:feesu
  • 7
  • 7
14 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34942481
0
 

Author Comment

by:feesu
ID: 34942558
Again leakim971, if the user is going to suffer to get this content, then let him suffer, but he won't get it easily.

You know, my page is going to be a set of Policies and Procedures that users should only read and not copy or download or even print. That's the whole idea.

Would you help me on achieving that? I don't mind if an expert interferes and compromise my page, as I know the users using my page are not of that type.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34942573
Yes I understand you, try this :
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>jQuery disable right click easily</title>
    <script language="javascript" type="text/javascript"
      src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
    </script>
    <script type="text/javascript" language="javascript">
        $(function() {
            $(this).bind("contextmenu copy cut paste", function(e) {
                e.preventDefault();
                alert("disabled!");
            });
        }); 
    </script>
</head>
<body>
    Sed lacus. Donec lectus. Nullam pretium nibh ut turpis.
    Nam bibendum. In nulla tortor, elementum vel, tempor at,
    varius non, purus. Mauris vitae nisl nec consectetuer.
    Donec ipsum. Proin imperdiet est. Phasellus dapibus semper
    urna. Pellentesque ornare, orci in consectetuer hendrerit,
    urna elit eleifend nunc, ut consectetuer nisl felis ac diam.
    Etiam non felis. Donec ut ante. In id eros.
</body>
</html>

Open in new window

0
 

Author Comment

by:feesu
ID: 34942602
Can't I also disable ctrl + A ?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34942633
Yes :


<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>jQuery disable right click easily</title>
    <script language="javascript" type="text/javascript"
      src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
    </script>
    <script type="text/javascript" language="javascript">
        $(function() {
            $(this).bind("contextmenu copy cut paste", function(e) {
                e.preventDefault();
                alert("disabled!");
            });
            $(this).keydown(function(e) {
							if(e.ctrlKey && e.keyCode == 65) {                         
                e.preventDefault();
                alert("disabled!");
							}
            });
        }); 
    </script>
</head>
<body>
    Sed lacus. Donec lectus. Nullam pretium nibh ut turpis.
    Nam bibendum. In nulla tortor, elementum vel, tempor at,
    varius non, purus. Mauris vitae nisl nec consectetuer.
    Donec ipsum. Proin imperdiet est. Phasellus dapibus semper
    urna. Pellentesque ornare, orci in consectetuer hendrerit,
    urna elit eleifend nunc, ut consectetuer nisl felis ac diam.
    Etiam non felis. Donec ut ante. In id eros.
</body>
</html>

Open in new window

0
 

Author Comment

by:feesu
ID: 34942687
Perfect, now if I wanted to prevent from highlighting and dragging the text into a Word file, do I use the <body ondragstart='return false' onselectstart='return false' >

Or does that have any gap or you recommend something else?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34942790
using : http://www.blograndom.com/blog/2009/07/jquery-plugin-to-disable-text-selection-on-elements/
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>jQuery disable right click easily</title>
    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
      <script language="javascript" src="http://code.jdempster.com/jQuery.DisableTextSelect/jquery.disable.text.select.js"></script>
    <script type="text/javascript" language="javascript">
        $(function() {
        
            $(this).bind("contextmenu copy cut paste", function(e) {
                e.preventDefault();
                alert("disabled!");
            });
            $(this).keydown(function(e) {
							if(e.ctrlKey && e.keyCode == 65) {                         
                e.preventDefault();
                alert("disabled!");
							}
            });

            $(this).disableTextSelect();
        }); 
    </script>
</head>
<body>
    Sed lacus. Donec lectus. Nullam pretium nibh ut turpis.
    Nam bibendum. In nulla tortor, elementum vel, tempor at,
    varius non, purus. Mauris vitae nisl nec consectetuer.
    Donec ipsum. Proin imperdiet est. Phasellus dapibus semper
    urna. Pellentesque ornare, orci in consectetuer hendrerit,
    urna elit eleifend nunc, ut consectetuer nisl felis ac diam.
    Etiam non felis. Donec ut ante. In id eros.
</body>
</html>

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:feesu
ID: 34942843
leakim971,

Disabling text select using the JQuery didn't work for Firefox, while the <body ondragstart='return false' onselectstart='return false' > thing did!

Try that in Firefox, it will allow you to select and drag to Word, but with the body thing, it will only allow Firefox to select but not drag, so that's better!
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34942913
good, so just use both :
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>jQuery disable right click easily</title>
    <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
      <script language="javascript" src="http://code.jdempster.com/jQuery.DisableTextSelect/jquery.disable.text.select.js"></script>
    <script type="text/javascript" language="javascript">
        $(function() {
        /*
            $(this).bind("contextmenu copy cut paste", function(e) {
                e.preventDefault();
                alert("disabled!");
            });
            $(this).keydown(function(e) {
							if(e.ctrlKey && e.keyCode == 65) {                         
                e.preventDefault();
                alert("disabled!");
							}
            });*/
            $(this).disableTextSelect();
        }); 
    </script>
</head>
<body ondragstart='return false' onselectstart='return false' >
    Sed lacus. Donec lectus. Nullam pretium nibh ut turpis.
    Nam bibendum. In nulla tortor, elementum vel, tempor at,
    varius non, purus. Mauris vitae nisl nec consectetuer.
    Donec ipsum. Proin imperdiet est. Phasellus dapibus semper
    urna. Pellentesque ornare, orci in consectetuer hendrerit,
    urna elit eleifend nunc, ut consectetuer nisl felis ac diam.
    Etiam non felis. Donec ut ante. In id eros.
</body>
</html>

Open in new window

0
 

Author Comment

by:feesu
ID: 34942931
- But do I still need the JQuery one if the other one works?
- Does JQuery help in preventing print-screen?
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
ID: 34942995
>But do I still need the JQuery one if the other one works?

for the other events, browser compatibilty (when it apply) yes but you right it's big to disable something...

>Does JQuery help in preventing print-screen?

With keyboard yes with the browser menu I don't think
0
 

Author Comment

by:feesu
ID: 35062806
Perfect, give me sometime and I shall get back to you. Thanks!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 35062815
np
0
 

Author Comment

by:feesu
ID: 35206433
I am sorry leakim971 for taking so much time on this, as the requirements of the management kept changing.
Thank you very much for you time and help!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now