如何检测回车键/或"GO"按钮.按钮在地址栏中? [英] How to detect ENTER keypress/ or the "GO" button in the Address Bar?

查看:49
本文介绍了如何检测回车键/或"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).

这篇关于如何检测回车键/或"GO"按钮.按钮在地址栏中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆