2015-11-13 68 views
0

我想創建一個按鈕,它將在使用javascript的新文檔中顯示瀏覽器的詳細信息。我已經在這裏搜索和w3schools,我很難過!我真的很新的JavaScript,所以任何建議,非常感謝。謝謝!Javascript - 在新文檔中顯示瀏覽器的詳細信息

<html> 
    <head> 
    <script type="text/javascript"> 
    function docOpen() 
    { 
    document.open(); 
    document.write(browserDetails); 
    } 
function browserDetails() { 
    var x = navigator 
    document.write("CodeName=" + x.appCodeName) 
    document.write("<br />") 
    document.write("MinorVersion=" + x.appMinorVersion) 
    document.write("<br />") 
    document.write("Name=" + x.appName) 
    document.write("<br />") 
    document.write("Version=" + x.appVersion) 
    document.write("<br />") 
    document.write("CookieEnabled=" + x.cookieEnabled) 
    document.write("<br />") 
    document.write("CPUClass=" + x.cpuClass) 
    document.write("<br />") 
    document.write("OnLine=" + x.onLine) 
    document.write("<br />") 
    document.write("Platform=" + x.platform) 
    document.write("<br />") 
    document.write("UA=" + x.userAgent) 
    document.write("<br />") 
    document.write("BrowserLanguage=" + x.browserLanguage) 
    document.write("<br />") 
    document.write("SystemLanguage=" + x.systemLanguage) 
    document.write("<br />") 
    document.write("UserLanguage=」 + x.userLanguage) 
    } 
    </script> 
    </head> 
    <body> 
    <form> 
    <input type="button" onclick="docOpen()" value="Get Browser Details"> 
    </form> 
    </body> 
+1

W3Schools的建議(見【規範】警告不良行爲,比如'document.write'(http://www.w3.org/TR/html5/webappapis:

我在做一個工作的小提琴html的#文件撰寫%28%29))。我建議避免這兩個。 – Oriol

+0

謝謝Oriol。我的大學教授給我提供了document.write作爲例子。這對於班級來說並不好。 lol –

回答

2

你有一個適當的標準(直)雙引號這裏的捲曲雙引號:

document.write("UserLanguage=」 + x.userLanguage) 
          ^

這導致一個語法錯誤。用直接的引號替換它。

0

你可以這樣做基於@Barmar評論

<script> 
    function docOpen() 
    { 
    document.open(); 
    browserDetails(); // ADD '()' to call the function -- 
    } 
function browserDetails() { 
    var x = navigator; 
    // iterate through all properties and get the values 
    for(var prop in x) { 
     document.write(prop+' = '+ x[prop]+'<br />'); 
    } 
    } 
    </script> 
+1

'browserDetails()'不返回任何東西,寫入返回值沒有意義。 – Barmar

+0

是真的!對不起, – TJB4rn3s

1

問題

<html> 
    <head> 
     <script> 
    function docOpen() 
    { 
    document.open(); 
    document.write(browserDetails()); // ADD '()' to call the function 
    } 
function browserDetails() { 
    var x = navigator; 
    // iterate through all properties and get the values 
    for(var prop in x) { 
     document.write(prop+' = '+ x[prop]+'<br />'); 
    } 
    } 
    </script> 
    </head> 
    <body> 
    <form> 
    <input type="button" onclick="docOpen()" value="Get Browser Details"> 
    </form> 
    </body> 

編輯是,你是不是要麼調用您所定義的功能。對browserDetails的調用不是一個調用,它只是一個參考,並沒有什麼是調用docOpen函數。

變更線4 document.write(browserDetails());

然後調用docOpen docOpen()

您還需要修復的智能報價由duskwuff的指示。 https://jsfiddle.net/87q1a0kn/

+1

'browserDetails()'不返回任何東西,將它作爲參數傳遞是沒有意義的。 – Barmar

+0

它不會因爲寫入文檔而返回任何內容。它仍然需要被調用 – danwellman

+0

是的,它需要被調用,但不能作爲'document.write()'的參數。只要做'browserDetails();'。 – Barmar