Solved

JavaScript - How do I disable printing my webpage?

Posted on 2011-02-21
14
288 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 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