Buoka Posted July 2, 2007 Share Posted July 2, 2007 Having had our pages "borrowed" by the right-click, view source syndrome, we wish to protect them a little in IE.The following code works a bit. The right click work at the top of the page but not the bottom 2/3. Why? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html> <head> <link rel="stylesheet" href="Style.css" type="text/css" media="screen" /> <title>Spa Showroom</title> <script type="text/javascript"> function disable() { if (event.button == 2) { alert("Sorry rightclick disabled on this page") } } </script> </head> <body class="spa2" onmousedown="disable()"> <br /><br /><br /><br /><br /> <table align="center" width="100%" cellspacing="50"> <tr align="center"> <td class="const">Welcome to our Spa Showroom</td> </tr> </table> </body></html> This code without the class="spa2" part works fully (in IE) on other pages.The class="spa2" in the Body really just adds a fixed picture background. H ere is that part of CSS in case it is relevant Body.spa2 { font-family: Arial, "Times New Roman", Sans-Serif; font-size: 10.5pt; background: #ffffff url('Images/Back_Spa2.jpg') no-repeat fixed center; background-attachment: fixed; color: #000000; text-align: justify; margin: .1in;} Can anyone help/advise please Link to comment Share on other sites More sharing options...
jesh Posted July 2, 2007 Share Posted July 2, 2007 This seems to work OK in IE. It might be of help: document.oncontextmenu = function() { return false; } Link to comment Share on other sites More sharing options...
Buoka Posted July 3, 2007 Author Share Posted July 3, 2007 This seems to work OK in IE. It might be of help:document.oncontextmenu = function() { return false; } Thanks Jesh.Errrrrrrr where should that code go and a brief explanation of what it does would be of great assistance as I am very new to Javascript.Thanks very much Link to comment Share on other sites More sharing options...
jesh Posted July 3, 2007 Share Posted July 3, 2007 You can put that in the head and it'll work for you: <html><head><script type="text/javascript">document.oncontextmenu = function() { return false; }</script></head><body><div>Right click on the page...</div></body></html> Now, what is it doing? In javascript, as in other programming languages, there is an event handling mechanism where, rather than having to write something that sits there and listens for a particular event to happen (in this case, the context menu appearing), the framework takes care of that listening and all you have to to is register a function to handle that event if you want to handle that event.So, if you wanted to have a little alert pop up each time the mouse was clicked, you could create a function that would put up an alert and then register that function as the event handler for the mouse down event: function myalert(){ alert("Mouse clicker!");}document.onmousedown = myalert; So, what we've done in the context menu example is to create a function that simply returns false. The reason this is important is that when you return true from an event handler, you are telling the scripting engine to finish processing the event. If you return false, the event processing stops at that point - effectively canceling the event.Therefore, when you assign an event handler to the contextmenu event that simply returns false, you are telling the browser that any time the context menu is supposed to be shown it should stop the event (and not show the menu).I hope this helps. Link to comment Share on other sites More sharing options...
Buoka Posted July 4, 2007 Author Share Posted July 4, 2007 You can put that in the head and it'll work for you:<html><head><script type="text/javascript">document.oncontextmenu = function() { return false; }</script></head><body><div>Right click on the page...</div></body></html> Now, what is it doing? In javascript, as in other programming languages, there is an event handling mechanism where, rather than having to write something that sits there and listens for a particular event to happen (in this case, the context menu appearing), the framework takes care of that listening and all you have to to is register a function to handle that event if you want to handle that event.So, if you wanted to have a little alert pop up each time the mouse was clicked, you could create a function that would put up an alert and then register that function as the event handler for the mouse down event: function myalert(){ alert("Mouse clicker!");}document.onmousedown = myalert; So, what we've done in the context menu example is to create a function that simply returns false. The reason this is important is that when you return true from an event handler, you are telling the scripting engine to finish processing the event. If you return false, the event processing stops at that point - effectively canceling the event.Therefore, when you assign an event handler to the contextmenu event that simply returns false, you are telling the browser that any time the context menu is supposed to be shown it should stop the event (and not show the menu).I hope this helps. Brilliant! thanks a bundle Jesh Link to comment Share on other sites More sharing options...
jesh Posted July 5, 2007 Share Posted July 5, 2007 Glad to have helped! Link to comment Share on other sites More sharing options...
kvnmck18 Posted July 6, 2007 Share Posted July 6, 2007 Just to note: There is no way to protect source code. There are many easy ways to get a copy of source code and especially if it's just being protected by a javascript.To protect design/content get your website copyrighted to include the design/colors/content....it's always worth the extra money to protect your work Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.