Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Keyup not working

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #41441
    Cathyk
    Participant

    I’m a novice when it comes to javascript and am trying to get the escape key to exit site quickly, following the https://css-tricks.com/website-escape/ post and using the script. Everything is working except the following code:

    $(document).keyup(function(e) {
    if (e.keyCode == 27) { // escape key
    getAway();

    Any help greatly appreciated.

    Thanks!
    Cathy

    #118043
    chrisburton
    Participant

    @Cathyk Hi. I’m not that familiar with Javascript either but do you have a link to your site so I can see if the console is throwing an error?

    #118044
    Cathyk
    Participant
    #118217
    JohnMotylJr
    Participant

    @Cathyk : First, if you plan on using jQuery then you need to reference it locally or through a cdn. Add this:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

    I slightly modified your code as well as included an example.

    function getAway() {
    window.open('http://omg.yahoo.com/news/','_newtab');
    window.location.replace('http://omg.yahoo.com/news/');
    }

    $(do​cument).keyup(function(e) {
    if (e.which === 27) {
    getAway();
    }
    });​

    jsFiddle because CodePen was being a weirdo. I hope this helps.

    #118286
    Cathyk
    Participant

    @John: THANK YOU! As I stated, I’m a novice & didn’t have the link for jQuery. Once I placed that in the file, it works!

    Enjoy your holidays!

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.