2012-10-11 95 views
1

我有一個網頁,有一些鏈接到PDF文件,打開一個新的scrren。如果我點擊其中一個鏈接,新的頁面就會打開。如果我在另一個鏈接點擊,而頁面打開它取代了新的一頁這是很好的頁面,但它拋出一個JS ERRE說:「找不到成員」Javascript「找不到成員」

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(); 
} 

的錯誤是在w.focus();

+0

我們可以有一個jsfiddle鏈接嗎? – MyBoon

+0

'focus()'函數不是標準的,也許你正在測試的瀏覽器不支持它。如果您想要對彈出窗口進行精細控制,最好使用DHTML。對於「幫助」彈出窗口,我個人非常喜歡只用'target =「_ blank」'打開一個新選項卡。這樣,新的窗口/標籤將默認爲焦點,如果用戶選擇使用瀏覽器的「在後臺打開」功能,則不會。替換之前彈出窗口內容的鏈接也很煩人。它' – millimoose

+1

當然,它是支持的,但只有當窗口存在。無論如何,1997年調用並希望它的JavaScript回來。 – mplungjan

回答

2

可怕地舊代碼。由於parms中的空格,它會寫入的瀏覽器失敗。這裏是文檔window.open Best practices 沒有必要區分頂部/左側和screenX,screenY - 後者是Netscape 4及更早的版本,如果您希望其他瀏覽器也可以指定,因爲它們會忽略它們不包含的參數需要。任何PARM =沒有可以刪除任何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(); } 
+0

謝謝...我應該只是jQuery?....我通常不工作的JS,但這降落在我的腿上 –

+0

jQuery不會有任何區別。你的代碼中沒有任何東西可以從jQuery中受益。 – mplungjan

0

這是因爲你的「w」變量的範圍在你聲明的函數內部欣。

爲了使它在下次調用時可訪問,您需要在函數外聲明您的w var。當你調用函數時,它會創建一個新的box/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(); 
} 

即使這不是非常有效,因爲如果窗口仍處於打開狀態,你就迫使每個呼叫重建整個窗口,當你可以簡單地告訴你,首先打開的窗口轉到新的網址。

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(); 
    } 
} 

現在,請注意,即使在這種重構的代碼,我們有一個問題......你確定「左」,「頂」打開窗戶時,這樣比網景其他所有瀏覽器使用?除此之外,甚至還有可用性問題和基於瀏覽器的彈出窗口阻止程序,這可能會影響您的代碼功能。

+1

要與舊版瀏覽器兼容,您需要刪除參數中的空格。另外document.location已被棄用,你應該使用window.location來編寫和document.URL來閱讀它 – mplungjan

+0

嘿,感謝您的信息......它已經很久,因爲我甚至對window.open()的困擾,我已經忘記了一些有趣的小型跨瀏覽器問題! :) –