Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-31-2007, 03:39 AM   PM User | #1
jmitch18
New Coder

 
Join Date: Mar 2007
Location: Northern Ireland
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
jmitch18 can only hope to improve
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..
jmitch18 is offline   Reply With Quote
Old 08-31-2007, 03:47 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 08-31-2007, 11:48 AM   PM User | #3
jmitch18
New Coder

 
Join Date: Mar 2007
Location: Northern Ireland
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
jmitch18 can only hope to improve
Thanks
jmitch18 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:25 PM.


Advertisement
Log in to turn off these ads.