如何隐藏 html 源代码并禁用右键单击和文本复制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6597224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide html source & disable right click and text copy?
提问by beeflavor
The following website has both right click and view source disabled.
以下网站已禁用右键单击和查看源代码。
http://www.immihelp.com/visitor-visa/sponsor-documents.html
http://www.immihelp.com/visitor-visa/sponsor-documents.html
Can anyone shine some light on how this is possible?
任何人都可以阐明这是如何可能的吗?
回答by Konrad Rudolph
The following website has both right click and view source disabled.
以下网站已禁用右键单击和查看源代码。
They fooled you. Just scroll down in view-source.
他们骗了你。只需在查看源中向下滚动即可。
Furthermore, employing such tactics marks you as unprofessional. Don't do it.
此外,使用此类策略会标记您不专业。不要这样做。
回答by Brad
They do this with some basic javascript, but this does not actually hide your HTML source! In many browsers you can simply go to view->source on the menu. Even if you couldn't, it is trivial to simply load up a debugging proxy like Fiddler, or packet-sniff the connection.
他们使用一些基本的 javascript 来做到这一点,但这实际上并没有隐藏您的 HTML 源代码!在许多浏览器中,您只需转到菜单上的查看-> 源代码即可。即使你不能,简单地加载像Fiddler这样的调试代理,或者包嗅探连接也是微不足道的。
It is impossible to effectively hide the HTML, JavaScript, or any other resource sent to the client. Impossible, and isn't all that useful either.
不可能有效地隐藏发送到客户端的 HTML、JavaScript 或任何其他资源。不可能,也不是那么有用。
Furthermore, don't try to disable right-click, as there are many other items on that menu (such as print!) that people use regularly.
此外,不要尝试禁用右键单击,因为该菜单上还有许多人们经常使用的其他项目(例如打印!)。
回答by Grezzo
It's a horrible thing to do, as everybody else has said, but if you really are intent on doing it, use this code, and put a load of returns at the top of the page's source:
正如其他人所说,这是一件可怕的事情,但如果您真的打算这样做,请使用此代码,并在页面源代码的顶部放置大量返回:
<html>
<head>
<script>
function disableClick(){
document.onclick=function(event){
if (event.button == 2) {
alert('Right Click Message');
return false;
}
}
}
</script>
</head>
<body onLoad="disableClick()">
</body>
</html>
回答by Tak
Hiding HTML source isn't really possible. Disabling right-click only frustrates users who wish to do something constructive with your content (copy/paste content or forms, or print, for example).
隐藏 HTML 源代码是不可能的。禁用右键单击只会让希望对您的内容做一些有建设性的事情(例如复制/粘贴内容或表单,或打印)的用户感到沮丧。
If you're running a server-side scripting language you could obfuscate or minify the HTML, CSS and Javascript. This will make it harder for someone to copy your code or see how you've achieved certain effects.
如果您正在运行服务器端脚本语言,您可以混淆或缩小 HTML、 CSS 和 Javascript 。这将使他人更难复制您的代码或查看您是如何实现某些效果的。
回答by Nikhil Dinesh
<body oncontextmenu="return false">
Use this code to disable right click.
使用此代码禁用右键单击。
回答by Taryn
You can still view the source on the website by going to View > Page Source from the toolbar in firefox. Or View > source in IE.
您仍然可以通过从 Firefox 的工具栏中转到“查看”>“页面源”来查看网站上的源。或者在 IE 中查看 > 源代码。
The right-click is disabled via javascript. The source for the javascript is:
通过javascript禁用右键单击。javascript的来源是:
回答by George Johnston
Believe me, no one wants your source as much as you may think they do. When you decided to develop web pages, you became an open source developer.
相信我,没有人像您认为的那样想要您的消息来源。当您决定开发网页时,您就成为了一名开源开发人员。
It's not possible to disable viewing a pages source. You can attempt to circumvent unknowledgeable users from seeing the source, but it won't stop anyone who understands how to use menu's or shortcut keys. Your best bet is to develop your site in a manner that will not be compromised by someone seeing your source. If you're attempting to hide it for any other reason than to protect your intellectual property, then you're doing something wrong.
无法禁用查看页面源。您可以尝试避开不知情的用户查看源代码,但它不会阻止任何了解如何使用菜单或快捷键的人。最好的办法是以一种不会被看到您的来源的人损害的方式开发您的网站。如果您出于保护知识产权之外的任何其他原因试图隐藏它,那么您就做错了。
回答by Jishnu V S
This code is used for disable the right click events and keyboard short cuts.
此代码用于禁用右键单击事件和键盘快捷键。
Just try with this code
试试这个代码
document.onkeydown = function(e) {
if(e.keyCode == 123) {
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)){
return false;
}
if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)){
return false;
}
}
回答by Prashant Bhate
You potentially can not prevent user from viewing the HTML source content. The site that you have listed prevents user from right click. but fact is you can still do CTRL+ Uin Firefox to view source!
您可能无法阻止用户查看 HTML 源内容。您列出的站点阻止用户右键单击。但事实是您仍然可以在 Firefox 中执行CTRL+ U来查看源代码!
回答by EmilF
View source is not disabled in my browser (Chrome).
我的浏览器 (Chrome) 中未禁用查看源代码。
But they have added a lot of blank lines to the source, so you have to scroll down to view it. Try to scroll down and you will see.
但是他们在源代码中添加了很多空行,因此您必须向下滚动才能查看。尝试向下滚动,你会看到。
the disabled right click is possible with javascript, but dont do it. Its very irritating for the user.
javascript 可以禁用右键单击,但不要这样做。它对用户来说非常刺激。