Solved

JavaScript - How do I disable printing my webpage?

Posted on 2011-02-21
14
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

691 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