SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast Josty's Avatar
    Join Date
    Jun 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Disable default actions when hitting for instance ctrl+b

    Hi, how to diasble the default actions, like bookmarks when typing ctrl+b? Or search when using ctrl+f...

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't for 2 reasons:

    1) Browsers in the end decide what the default actions are
    2) Someone can disable javascript

    Also checking for CTRL+B is somewhat annoying.

  3. #3
    SitePoint Enthusiast Josty's Avatar
    Join Date
    Jun 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hum, they have done this at codingforums.com. I just can't figure out how...

  4. #4
    SitePoint Enthusiast Josty's Avatar
    Join Date
    Jun 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Though more testing revials that it doesn't work in explorer... Just firefox.

  5. #5
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The site below has an example of how to do this. I experimented with it and I was able to disable many key clicks like F11 & CTRL-B in IE. I didn't try it in FF.

    http://www.alphafilter.com/?inc=article&aid=69

  6. #6
    SitePoint Enthusiast Josty's Avatar
    Join Date
    Jun 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks darkwater23! I also got this to work in explorer. Some more puzzeling with it, and perhaps I'll get it to work in Firefox as well.

  7. #7
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the question I ask is why. I hope there's a better way to do this, because as stated, it may work in explorer, but if you've got, say Opera installed, it stays above JavaScript with regards to key combinations. That's why I'm against it.

  8. #8
    SitePoint Enthusiast Josty's Avatar
    Join Date
    Jun 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So that you can hit ctrl+b to get bold text, for instance! So neat! And for opera: How many are using it again?

  9. #9
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "How many people are using it?" should not be a question asked by a web developer. The only time I make an exception is if the amount of users that use the browser is small AND it's totally broken. To further your question, you have stated the code doesn't work in firefox. Let me then ask you "How many people are using Firefox"?

  10. #10
    SitePoint Enthusiast Josty's Avatar
    Join Date
    Jun 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firefox? What is that? Nono, I see your point! I always like to make sure things work in at least IE, Firefox and Opera. (I'm not aware of any else "big" browser, commonly used in Norway, but if anyone knows about some more I really should check agains, please let me know.)

    But I don't think this is such a big issue, when all I want to accomplish is disable some commands, like ctrl+b and ctrl+i and ctrl+u, only, and only then, when a certain textarea has focus. Want you agree?

    Thanks anyway for the responses!

  11. #11
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,593
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Around 0.5% - 2% of people are using Opera - it varies beween sites. For a typical web site that gets 500,000 visitors a month this equates to somewhere between 2,500 and 10,000 people a month who visit a typical web site using the Opera browser. This is NOT an insignificant number of visitors plus they are usually amongst the most web literate of your visitors.

    Opera and Firefox (and Safari as well) handle things fairly similary since they all follow the same standards and only differ in whether or not they have implemented leading edge features. Most code should therefore work as well or as poorly for all of these browsers.

    I don't think that trying to override the shortcuts that your visitors have set up is an appropriate thing to do even if you do want to ignore those browsers that correctly disallow your code. If I set up the ctrl-b shortcut to open a popup window so that I can write a blog about something that your web page reminded be about then I don't want your page trying to block me from doing so as it is none of your business.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  12. #12
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL! Over the months, I've posted this link to other forums and probably other threads on this forum when this question comes up. Each time I do, I feel like I opened Pandora's box!

    My two cents on it is this: I never use Javascript for anything except image rollovers and form validation. Anything else can be difficult to make work in all browsers. But, the author of the article makes corporate web apps exclusively for IE, not web sites. So, Josty will have to decide who his audience is and if excluding some users because of their browser is acceptable to get the functionality he wants.

    Good luck with your project, Josty! I hope it goes well.

  13. #13
    SitePoint Enthusiast Josty's Avatar
    Join Date
    Jun 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! Laughing now...

  14. #14
    SitePoint Enthusiast Josty's Avatar
    Join Date
    Jun 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My solution

    After my vacation, I spent a couple more hours on this, and now I have working solution for at least Firefox (tested on 1.5) and Explorer (testet on 6). chris_fuel was right, I wasn't able to get it to work on Opera. Even though I'm from Norway, I don't take that as a big failure

    HTML Code:
    <textarea rows="10" cols="30" onkeydown="test(event);" ></textarea>
    	
    
    <script type="text/javascript">
    <!--
    function test(e)
    {
    	// Ctrl-variations
    	if (e.ctrlKey)
    	{
    		switch(e.keyCode)
    		{
    			case 66: // b
    			case 70: // f
    			case 73: // i
    			case 75: // k
    			case 85: // u
    				disableDefault(e);
    			break;
    		}
    	}
    	
    	// F5
    	else 
    	{
    		switch(e.keyCode)
    		{
    			case 116: // F5
    				disableDefault(e);
    			break;
    		}
    	}
    }
    
    function disableDefault(e)
    {
    	
    	// FF
    	if (e.preventDefault)
    	{
    		e.preventDefault();
    	}
    		
    	// MSIE
    	else
    	{
    		e.returnValue = false;
    		// Fix som stuborn events, like F5 and ctrl-f
    		e.keyCode = 0; 
    	}
    	
    	return;	
    }
    -->
    </script>
    Just save this in an html-file and you are ready to test it.

    (Hope I don't get shoot...)


Bookmarks

Posting Permissions

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