2013-04-08 56 views
1

此腳本打開一個輸入網站的新頁面。我怎麼操縱它出現在通過複選框的窗口?例如,如果檢查滾動條的複選框,我如何將它添加到新窗口?如何通過複選框將滾動條添加到新窗口?

<!DOCTYPE html> 
<html> 
    <head> 
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> 
    <title></title> 

    <script> 


      function go(){ 

       var myWindow; 
       var urlValue = document.getElementById("urlValue").value; 

        //radio buttons 
        if(yes.checked){ 

         myWindow = window.open(urlValue); 
        } 

        else if (no.checked){ 
         myWindow = window.open(urlValue, "_self"); 
         } 



        //checkbox loop 
        /* 
        for (i=0; i<document.myForm.checkGroup.length; i++){ 
         if (document.myForm.checkGroup[i].checked==true) 
         alert("Checkbox at index "+i+" is checked!") 
         } 
        */ 

       } 

    </script> 

    </head> 
    <body> 
    <form name="myForm"> 

     <p><label>Enter URL with http:// here: </label> <input id="urlValue" type="text"></p> 
     <p><label>Would you like to open a new window?<br> 
      <input name="yesNo" id="yes" type="radio">Yes<br> 
      <input name="yesNo" id ="no" type="radio">No </label></p> 
     <p><label>Other Options</label> <br> 
     <input name = "checkGroup" id="fullScreen" type="checkbox">Full Screen<br> 
     <input name = "checkGroup" id="scrollBar" type="checkbox">Scroll Bar<br> 
     <input name = "checkGroup" id="statusBar" type="checkbox">Status Bar<br> 
     <input name = "checkGroup" id="toolBar" type="checkbox">Tool Bar<br> 
     </p> 
     <p><input id="goButton" value="GO" type="button" onclick = "go()"></p> 
    </form> 

    </body> 
</html> 

回答

0
<!DOCTYPE html> 
<html> 
<head> 
    <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type" /> 
    <title>Window opener</title> 
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     var windowOpenerForm; 

     windowOpenerForm = $('#windowOpenerForm'); 

     windowOpenerForm.submit(function (e) { 
     var url, 
      isNewWindow, 
      windowOptions, 
      windowTarget, 
      windowSpecs; 

     e.preventDefault(); 

     url = 'http://' + $('#urlValue', windowOpenerForm).val(); 
     isNewWindow = $('input[name="openNewWindow"]:checked', 
         windowOpenerForm).val(); 
     windowOptions = extractWindowOption(
          $('input[name="windowOptions"]', 
          windowOpenerForm)); 
     windowTarget = isNewWindow === 'yes' ? '_blank' : '_self'; 
     windowSpecs = [ 
      'fullscreen=' + windowOptions.fullScreen, 
      'scrollbars=' + windowOptions.scrollBar, 
      'status=' + windowOptions.statusBar, 
      'toolbar=' + windowOptions.toolBar 
      ].join(','); 

     window.open(url, windowTarget, windowSpecs); 
     }); 

     function extractWindowOption(winOptions) { 
     var opt = {}; 

     winOptions.each(function (index, elem) { 
      opt[$(elem).val()] = $(elem).is(':checked') ?'yes' : 'no'; 
      }); 

     return opt; 
     } 
    }); 
    </script> 
</head> 
<body> 
    <form name="myForm" id="windowOpenerForm"> 
    <p> 
     <label>Enter URL with http:// here: </label> 
     <input id="urlValue" type="text"> 
    </p> 
    <p>Would you like to open a new window?<br> 
     <input name="openNewWindow" type="radio" value="yes" checked>Yes<br> 
     <input name="openNewWindow" type="radio" value="no">No 
    </p> 
    <p>Other Options<br> 
     <input name="windowOptions" value="fullScreen" type="checkbox">Full Screen<br> 
     <input name="windowOptions" value="scrollBar" type="checkbox">Scroll Bar<br> 
     <input name="windowOptions" value="statusBar" type="checkbox">Status Bar<br> 
     <input name="windowOptions" value="toolBar" type="checkbox">Tool Bar<br> 
    </p> 
    <p><input value="GO" type="submit"></p> 
    </form> 
</body> 
</html> 
1

這種方法在現代瀏覽器中棄用......

有不再狀態欄隱藏起來,你再也無法掩飾滾動條打開網頁全屏也無法像在舊版瀏覽器中那樣工作,現在有一個瀏覽器API可用於將瀏覽器切換到全屏模式,但您無法通過父窗口在window.open()中執行該操作。

下面是如何將舊的瀏覽器的示例:
http://jsfiddle.net/MyajZ/

在現代瀏覽器中仍然打開與沒有工具欄新的窗口,並切換到全屏模式,而是立即它可以追溯到正常狀態......

順便說一句,我發現這種管理更簡單的方法複選框的:

var params = ""; 
params += scrollBar.checked ? "scrollbars=yes," : "scrollbars=no,"; 
params += statusBar.checked ? "status=yes," : "status=no,"; 
params += toolBar.checked ? "toolbar=yes" : "toolbar=no"; 
params += fullScreen.checked? ",type=fullWindow,fullscreen" : ""; 

// ... 
myWindow = window.open(urlValue, "Window Name", params); 

變量的示例結果params

scrollbars=no,status=no,toolbar=no,type=fullWindow,fullscreen 
相關問題