简体   繁体   中英

Open new window/tab in which right click option is disabled (JavaScript)

I need to open a New Window/Tab on a hyperlink click where the right click is disabled in the newly opened window. How to achieve this in either JavaScript or JQuery? Can pass control to JavaScript on OnClick() event and call window.open() , but need to restrict right click on the new window..

If the new page that you are opening is yours , then you can put some JavaScript on that page to prevent the right click functions:

<script type="text/javascript"> 

var message="Sorry, right-click has been disabled"; 

function clickIE() 
{
    if (document.all) {(message);return false;}
}

function clickNS(e) 
{
    if (document.layers||(document.getElementById&&!document.all)) 
    { 
       if (e.which==2||e.which==3) {(message);return false;}}
    } 
if (document.layers) 
{
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown=clickNS;
} 
else
{
    document.onmouseup=clickNS;
    document.oncontextmenu=clickIE;
} 
document.oncontextmenu = new Function("return false");

</script> 

Like so: How do I disable right click on my web page?

If the page is not yours , then in general you cannot prevent the right click functions on that page. Even if you display the other page in an iFrame, it is still not possible to prevent rightclicking as you can see here:

Hence it is only possible if the new page is on your domain

This brings up another 'disputable' idea which is copying the source file of the url the user is trying to open to your own domain when the request is made, then adding some JavaScript to that source and then displaying that (your url) page instead of the original page. This can be, however, in conflict with copyright laws and it unneccesarily complicates the problem.

Mind you that it is highly discouraged to disable the right click as it will only irritate your users and it will not prevent them from copying anything (as there are always ways around these 'disabling scripts' eg by disabling the execution of JavaScript on that page or just looking at the source code).

Hence you shouldn't bother about trying to disable right mouse clicks.

If you don't want your users to be able to copy information from your website, simply don't put it there.

This is because of the way the internet was once constructed. Read more here

Simply speaking: if users know your website domain, they know your domain address and hence they can open/ retrieve all (open) files that are linked to this address (in practice this is not always true due to some smart tricks people have developed over the years).

The internet is just a huge collection of addresses, where each address is a portal to data you can download. Hence trying to protect a file through client side JavaScript is pretty ignorant because it neglects the fact that all files can be downloaded/ viewed anyway.

IMO, it is only usefull to irritate/ scare off some users that don't know what JavaScript is.

I hope this answers your question, but also makes you reconsider your original intention. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM