Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keyboard shortcuts with JavaScript
#5
I know jQuery is ok, I like it too. But in this case I am forced to stick to Prototype or raw JavaScript as I don't want to force users to load jQuery just for a fancy keyboard shortcut. I don't think it's worth it honestly.

Regarding the jQuery method, it's not working for me. I replaced $(".slide div:nth-child("+page+")").toggle(); with the given code and added anchor tags instead of the 3 divs. But it just doesn't work. Probably just a very silly mistake on my part. Here is the current markup I'm using:

Code:
<!DOCTYPE html>
    <head>
        <title>{$TITLE}</title>
        <link rel="stylesheet" type="text/css" href="style/main.css" />
        <script src="http://code.jquery.com/jquery-1.5.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function()
            {
                var page = 1;
                var pages = 0;
                var title = null;
                $.each($(".slide div"), function()
                {
                    $(this).css({"width":"100%", "height":"100%", "position":"absolute", "display":"block", "font-size":28});
                    $(this).toggle();
                    pages++;
                });
                
                $(".slide div:nth-child("+page+")").css("font-size", 28);
                $(body).load($(".slide div:nth-child("+page+")").attr("href"));
                
                $(this).bind("keydown", function(event)
                {
                    var key = event.keyCode;
                    if(key == 37)
                    {
                        $(body).load($(".slide div:nth-child("+page+")").attr("href"));
                        page = (page > 1) ? (page - 1) : 1;
                        $(body).load($(".slide div:nth-child("+page+")").attr("href"));
                    }
                    else if(key == 39)
                    {
                        $(body).load($(".slide div:nth-child("+page+")").attr("href"));
                        page = (page < pages) ? (page + 1) : pages;
                        $(body).load($(".slide div:nth-child("+page+")").attr("href"));
                    }
                });
            });
        </script>
        <!--[if IE 8]><link rel="stylesheet" href="style/ie.css" /><![endif]-->
        <!--[if IE 7]><link rel="stylesheet" href="style/ie.css" /><![endif]-->
        <!--[if IE 6]><link rel="stylesheet" href="style/ie.css" /><![endif]-->
    </head>
    <body>
        <div class="slide">
            <a href="keyboard_shortcut.html" title="Page 1">Sometext1</a>
            <a href="keyboard_shortcut2.html" title="Page 2">Sometext2</a>
            <a href="keyboard_shortcut3.html" title="Page 3">Sometext3</a>
        </div>
    </body>
</html>

Once again, thank you! Smile
Reply


Messages In This Thread
RE: Keyboard shortcuts with JavaScript - by rockclimber66 - 04-15-2011, 12:37 PM
RE: Keyboard shortcuts with JavaScript - by Gonzo - 04-18-2011, 07:10 PM
RE: Keyboard shortcuts with JavaScript - by Danny - 05-07-2011, 01:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Free Worker HTML, CSS, Javascript ImFocuzz 2 1,281 04-05-2014, 10:01 PM
Last Post: VHPanda
  [MyBBError Message] PLEASE HELP ME!! Enable Javascript Registration Error iRun 4 1,921 01-26-2012, 03:13 PM
Last Post: iRun
  Close Tab With Javascript. Qua 3 1,410 07-02-2011, 07:25 AM
Last Post: djakariuz
  Javascript Help. Form Submission. JohnRonder 0 890 02-01-2011, 11:12 AM
Last Post: JohnRonder
  [javascript] Link of Minute zone 3 1,093 12-11-2009, 02:16 AM
Last Post: Sagittarius

Forum Jump:


Users browsing this thread: 7 Guest(s)