如何检测回车键/或"GO"按钮.按钮在地址栏中?

问题描述:

我想使用Javascript检测地址栏的ENTER键,以及转到(转到指定的URL)"按钮.按照我以前的尝试,使用"keycode == 13"无法按要求工作.在下面的代码中说:

I want to detect the ENTER keypress of the Address Bar and also, the "Go(to the specified URL)" button using Javascript. As per my previous efforts using "keycode==13" did not work as required. say, in the following code:

window.onkeypress = testKeyEvent;

function testKeyEvent(e) {
    if (e.keyCode == 13) //We are using Enter key press event for test purpose.
    {
        alert('Enter key pressed');
    }
    else //If any other button pressed.
    {
        alert('Not Enter key pressed');
    }
}    </script>

我想先显示警报框,然后在地址框中键入任何URL(有效或无效),然后按ENTER/单击GO按钮,然后转到指定的URL.

I want first the Alert box to be displayed,after I have typed any URL(valid or not) in the address box and Pressed ENTER/ Clicked GO button and then go to specified URL.

有可能吗?我知道我错过了很多事情,请提一下.

Is it Possible? I know I am missing out on a lot of things, Please mention about them.

如果我正确地解释了您的问题,我认为您无法执行此操作,因为JavaScript运行所在的上下文在Document(即,JavaScript甚至都不知道浏览器本身是否存在.

If I am interpreting your question correctly, I don't think you can do this, because the context in which the JavaScript runs stops at the Document (meaning, JavaScript doesn't even quite know that the browser itself exists).