1

I have a web page that has some links to pdf files that open in a new scrren. If I click one of the links the new page opens fine. If I click on another link while that page is open it replaces the page with the new page which is fine, however it throws a JS erre saying "member not found"

HTML:

helpMenu.add( new AnchorMenuItem("User Guide", "javascript:openHelpWindow('../html/help/user_guide.html');") );
helpMenu.add( new AnchorMenuItem("FAQ", "javascript:openHelpWindow('../html/help/faq.html');") );
helpMenu.add( new AnchorMenuItem("Features", "javascript:openHelpWindow('../html/help/features.html');") );
helpMenu.add( new AnchorMenuItem("Overview", "javascript:openHelpWindow('../doc/Overview.pdf');") );
helpMenu.add( new AnchorMenuItem("Actual Info Guide", "javascript:openHelpWindow('../doc/ActualInfoGuide.pdf');") );

JS

/**
* Function to open the pop-up windows that the radio button
 * or select box options will be chosen from.
 */
function openHelpWindow(url)
{
    var w;
    if (isBrowserNetscape)
    {
        // Netscape
        w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, screenX=100, screenY=100");
    }
    else
    {
        // IE
        w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, left=100, top=100");
    }
    w.focus();
}

The error is at w.focus();

4

2 回答 2

2

可怕的旧代码。由于参数中的空格,它会在为其编写的浏览器中失败。这是文档window.open 最佳实践 无需区分顶部/左侧和 screenX,screenY - 后者是 Netscape 4 和更早版本,如果您希望其他浏览器也可以指定,因为它们会忽略参数他们不需要。可以删除任何 parm=no,并且可以将任何 parm=yes 设置为只是 parm:

简单版:

function openHelpWindow(url) {
   var w = window.open(url, "MSSTHelp", 
       "resizable,scrollbars,height=700,width=900,screenX=100,screenY=100,left=100,top=100");
   if (w) w.focus();
}

根据上述文档重用已经打开的窗口

var w; // global var
function openHelpWindow(url) {
   if (!w || w.closed) {
     w = window.open(url, "MSSTHelp",
        "resizable,scrollbars,height=700,width=900,screenX=100,screenY=100,left=100,top=100");
   }
   else {
     w.location=url; // OR window.open(url, "MSSTHelp"); 
   }
   if (w) w.focus();
}
于 2012-10-11T17:33:17.293 回答
0

这是因为您的“w”变量的范围在您声明它的函数内。

为了使其在下一次调用时可以访问,您需要在函数外部声明您的 w var。当您调用该函数时,它会在 /inside/ 函数中创建一个名为“w”的新框,它将引用粘贴到您使用 window.open() 创建的窗口。然后,当函数完成执行时,它会将“w”扔掉。在函数范围之外声明“w”是 /better/,但仍然可能有问题(因为现在您处于全局范围内)。

var w = null;
/**
* Function to open the pop-up windows that the radio button
* or select box options will be chosen from.
*/
function openHelpWindow(url)
{
    if (isBrowserNetscape)
    {
        // Netscape
        w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, screenX=100, screenY=100");
    }
    else
    {
        // IE
        w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, left=100, top=100");
    }
    w.focus();
}

即使这也不是非常有效,就好像窗口仍然打开一样,当您可以简单地告诉您首先打开的窗口转到新的 url 时,您会为每次调用强制重建整个窗口。

var w = null;
/**
* Function to open the pop-up windows that the radio button
* or select box options will be chosen from.
*/
function openHelpWindow(url)
{
    if(w == null){
        if (isBrowserNetscape)
        {
            // Netscape
            w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, screenX=100, screenY=100");
        }
        else
        {
            // IE
            w = window.open(url, "MSSTHelp", "resizable=yes, scrollbars=yes, menubar=no, location=no, toolbar=no, height=700, width=900, left=100, top=100");
        }
    } else {
        w.document.location(url);
        w.focus();
    }
}

仍然没有效率!看看所有的代码重复!

var w = null;
/**
* Function to open the pop-up windows that the radio button
* or select box options will be chosen from.
*/
function openHelpWindow(url)
{
    if(w == null){
        var winSpecs = "resizable=yes,scrollbars=yes,menubar=no," 
                     + "location=no,toolbar=no,height=700,width=900,"
                     + (isBrowserNetscape) ? "screenX=100,screenY=100" : "left=100,top=100";
        w = window.open(url, "MSSTHelp", winSpecs);
    } else {
        w.location(url);
        w.focus();
    }
}

现在,请注意,即使在这个重构的代码中,我们也有一个问题......你确定除了 Netscape 之外的所有浏览器在打开这样的窗口时都使用“left”和“top”吗?除此之外,甚至还有可用性问题和基于浏览器的弹出窗口阻止程序,这可能会干扰您的代码功能。

于 2012-10-11T17:39:17.090 回答