2010-12-14 54 views
1

並感謝您看看這個看似簡單的問題。在HTML之間導航

情況如下:我有一個名爲'welcome.html'的網站主頁。從這裏用戶可以從URL列表中進行選擇,並在URL上單擊一個簡單的Javascript函數,該函數將打開一個新窗口。

一旦用戶完成了新打開的窗口,我希望他們點擊一個調用Javascript函數的按鈕,將它們返回到主頁。看起來很簡單。

但是,如果主窗口仍處於打開狀態,我想返回到此狀態而不打開另一個顯示主頁的新窗口。如果主窗口已關閉,我想要一個新窗口打開顯示主頁。

我將非常感謝這裏的一些指導,因爲我似乎無法讓它工作。

問候

克里斯

+4

打開各種窗口的界面不是很友好 - 考慮一個不同的設計。 – Oded 2010-12-14 17:09:14

+0

您應該能夠通過跟蹤窗口名稱來完成此操作。但不要這樣做。您無法控制瀏覽器顯示新窗口的方式,或者即使它們顯示窗口(換句話說,使用製表符)。你也無法控制窗口焦點策略。總的來說,爲人們開設大量新窗口並不是一個好主意。 – Pointy 2010-12-14 17:09:36

+2

聽@Oded,因爲他說的是實話。 – 2010-12-14 17:11:00

回答

1

正如其他人所說,這是不是最大的設計。但是,過去我遇到過類似的情況,業務邏輯規定必須打開一個新窗口,並且無法更改此窗口。

由於尖說,最重要的事情就是保持跟蹤打開一扇窗戶,如果你需要一些進一步的幫助,這也許可以幫助: http://www.quirksmode.org/js/popup.html

但如果可能的話,我會考慮不同的設計(在這裏問你是否需要幫助實施它)

+0

謝謝你和其他人,感謝你的想法。我知道這不是一個好的模式,但我必須在將所有內容導入同一個窗口之前收拾當前的結構。我已經開發了一個解決方案,我將在下面分享一個答案給我自己。 – Chris 2010-12-16 09:31:46

1

並且謝謝大家誰去分享他們的想法的麻煩。

我解決了這個問題通過以下方式:

當從首頁導航到新的窗口下面的JavaScript來打開一個新窗口:

function popupFull(url) 

// For explanation of this code see: http://www.quirksmode.org/js/popup.html 

// Note: If fullscreen = 1 you can't see the menubar, toolbar, status etc. 
// It is advisable to have no spaces around the commas in the parameters. 

{ 
    //alert("Opening: " + url) 

    // Prepare the parameter string 
    params = 'width='+screen.width; 
    params += ',height='+screen.height; 
    params += ',top=0,left=0'; 
    params += ',fullscreen=0'; 
    params += ',menubar=0'; 
    params += ',toolbar=0'; 
    params += ',directories=0'; 
    params += ',status=0'; 
    params += ',scrollbars=0'; 
    params += ',resizable=1'; 

    // Open a new window. 
    newWin=window.open(url, "fullWindow", params); 

    // If the current Window is in focus, switch focus to the new Window. 
    if (window.focus) 
    { 
     newWin.focus() 
    } 

    // Return the new Window object reference. 
    return newWin; 
} 

所以新的窗口可以成爲打開並且我已經打開了主窗口,但是沒有關注新窗口的焦點。

在新窗口中有一個「菜單」按鈕。點擊這個調用下面的JavaScript函數:

function openMenu(winURL, winName, winFeatures) 
{ 
// Create a reference of the Window which opened this Window. This should be 
// the Main Menu Window. 
var winObj=window.opener; 

var menuOuterWidth = 1080; 
var menuOuterHeight = 896; 

var menuInnerWidth = 1068; 
var menuInnerHeight = 767; 

var menuX = (screen.width - menuOuterWidth)/2; 
var menuY = (screen.height - menuOuterHeight)/2; 

// Prepare the parameter string for re-opening the Menu 
params = 'width='+menuInnerWidth; 
params += ',height='+menuInnerHeight; 
params += ',top='+menuY+',left='+menuX; 
params += ',fullscreen=0'; 
params += ',menubar=1'; 
params += ',toolbar=1'; 
params += ',status=1'; 
params += ',scrollbars=1'; 
params += ',location=1'; 
params += ',resizable=1'; 

try 
{ 
    // Check to see if the window reference already exists. 
    if (winObj) 
    { 
     // Check to see if the Menu window is closed. 
     if (winObj.closed) 
     { 
      // The Menu window is closed. 
      // Open the Menu Window. 
      winObj = window.open(winURL, winName, params); 

      // Close this Course Window. 
      window.close(); 

      // Return the Menu Window object reference should the caller want it. 
      return winObj; 
     } 
     else 
     { 
      // The Menu Window has not been closed. Set the Window's size and position. 
      // Note: When resizing the outerWidth/outerHeight value has to be passed. 
      winObj.window.resizeTo(menuOuterWidth, menuOuterHeight); 
      winObj.window.moveTo(menuX, menuY); 

      // Bring it into focus (bring to front). 
      winObj.focus(); 

      // Close this Course Window. 
      window.close(); 

      // Return the Menu Window object reference should the caller want it. 
      return winObj; 
     } 
    } 

    else 

    { 

     // The winObj object does not exist. Open the Menu. 
     winObj = window.open(winURL, winName, params); 

     // Close this Course Window. 
     window.close(); 

     // Return the Menu Window object reference should the caller want it. 
     return winObj; 
    } 
} 

catch(err) 
{ 
    txt="There was an error on this page.\n\n"; 
    txt+="Error description: " + err.description + "\n\n"; 
    txt+="Click OK to continue.\n\n"; 
    //alert(txt); 

    // When IE6 tries to obtain the winObj.closed property, when the window is closed, it can cause 
    // an error "Permission Denied". This error is caught here. Open the Menu. 

    // Open the Menu Window. 
    winObj = window.open(winURL, winName, params); 

    // Close this Course Window. 
    window.close(); 

    // Return the Menu Window object reference should the caller want it. 
    return winObj; 
} 

}

的意見應說明一切。關鍵是要獲得我們開始的主頁窗口的參考。 (var winObj=window.opener;)。

讓我頭疼的是,如果我打開新窗口(使用IE6),切換回主頁並關閉主窗口,然後在新窗口中點擊'菜單'按鈕,什麼也沒有發生!我嘗試了一切,然後在喝了一杯茶之後,意識到我絕不會在任何應用程序中編寫代碼,而無需任何形式的錯誤捕獲。我添加了一個Try,Catch語句,並在「提醒」中報告了錯誤。我收到了'Permission Denied'錯誤。

經過大量的閱讀後,我想我無法消除錯誤,我會盡可能優雅地處理錯誤。這導致了上面的代碼。

它的作品,我希望這可以幫助別人。