Search code examples
javascriptshortcut

How to redirect page to url by shortcut key


I have code for shortcut key and when i click on that key it opens the url in a new window, but I want to have my current page be redirected to that url; the url should open in the same page.

<script type = "text/javascript">
    $(document).keyup(function (e) {
        var keyCode = e.keyCode ? e.keyCode : e.which
        if (keyCode == 17&&81) {
            window.open("urltest.html");
        }
    });
</script> 

Solution

  • To change the url of the current page, simply assign to window.location

    if (keyCode == 17&&81) {
        window.location = "urltest.html";
    }