Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2007
    Location
    Northern Ireland
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Preventing users from highlighting images

    I want to prevent users from highlighting images on a website I'm currently making just because it seems a bit more professional when they can't accidentally highlight any images used for the design.

    I have the following javascript which is supposed to work (found it after some googling):

    Code:
    function DisableSelection()
    		{
    			//prevent selecting text in IE
    			document.onselectstart = new function(){return "false"};
    
    			//prevent selecting text in FF
    			if (window.sidebar)
    			{
    				document.onmousedown = DisableText;
    				document.onclick = EnableText;
    			}
    		}
    
    		function DisableText(e)
    		{
    			return false;
    		}
    
    		function EnableText()
    		{
    			return true;
    		}
    I also have onMouseDown="DisableSelection()" inside the tag of each image I don't want them to be able to highlight.

    Anyone have any idea why this doesn't work?

    Thanks in advance.
    Last edited by jmitch18; 08-31-2007 at 03:41 AM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The code in the disable selection function adds the functionality that you want to the entire page when the function is run so by calling it from onmousedown you are only activating it when someone first highlights an image. It is then active for the whole page and not just the images.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Mar 2007
    Location
    Northern Ireland
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •