Javascript 中的 IE 上下文菜单项

IE Context Menu Item In Javascript

我正在尝试创建一个指向 Javascript html 文件的 IE 上下文菜单项,如此处所述 https://msdn.microsoft.com/en-us/library/bb735853(v=vs.85).aspx#IEAddOnsMenus_topic1 在 "Adding to a context menu" 部分下。我在 HKCU\Software\Microsoft\Internet Explorer\MenuExt 中列出了上下文菜单条目 它指向一个包含 javascript 的 html 文件。这是我正在使用的 Javascript 代码。

<script language="JavaScript">

function pausescript(ms) {
ms += new Date().getTime();
while (new Date() < ms){}
}      
{
var win = window.open("http://www.example.com");    
pausescript(2000);      
win.close();

}     
</script>

我正在尝试向 url 弹出一个 window,然后等待 2 秒并关闭 window。它正在工作,但是当它关​​闭弹出窗口时 window 由于某种原因 IE 失去焦点,除 IE 之外的任何其他 window 重新获得焦点,即使我从 IE 上下文菜单强制弹出。弹窗window关闭后如何让IE获得焦点?

您的问题是如何设置注册表。我使用 windows 8.1 并且我这样设置注册表:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\&实时搜索] @="C:\Usr\Whosebug\livesearch.htm" "Contexts"=双字:00000001

一切正常(您的脚本)。

您可以看到 Contexts 的值与指南中描述的值不同(我使用 1 而不是 0x10)。

说重启浏览器也没用

对焦问题

关闭 window 弹出窗口后 window IE 失去焦点。

MSDN 中所述,您需要访问 external.menuArguments 属性 以获取当前 ie 的 window 处理程序。

所以javascript代码是:

<script language="JavaScript">

    function pausescript(ms) {
        ms += new Date().getTime();
        while (new Date() < ms){}
    }
    {
        var win = window.open("http://www.example.com");
        pausescript(2000);
        win.close();
        try {
            // access the current browser window
            var parentwin = external.menuArguments;

            // get the document element
            var doc = parentwin.document;

            // focus it
            doc.body.focus();
        } catch(ex) {
            alert(ex);
        }
    }
</script>