简体   繁体   中英

javascript navigator objects not showing in alert box

I've tried many time to solve this but, I'm get entire code in alert box.

Can anyone solve this?

function detail(){
        var txt="";
        txt ="<p>Browser CodeName: "+ navigator.appCodeName +"</p>";
        txt +="<p>Browser Name: "+ navigator.appName +"</p>";
        txt +="<p>Browser Version: "+ navigator.appVersion +"</p>";
        txt +="<p>Cookies Enabled: "+ navigator.cookieEnable +"</p>";
        txt +="<p>Browsr Language: "+ navigator.language +"</p>";
        txt +="<p>Browser Online: "+ navigator.onLine+"</p>";
        txt +="<p>Platform: "+ navigator.platform +"</p>";
        txt +="<p>User-agent header: "+ navigator.userAgent +"</p>";
        txt +="<p>User-agent language: "+ navigator.systemLanguage +"</p>";

        alert(txt);
    }

When user click on button it shows alert with navigator details of browser

    <input type="button" value="Detail" onclick="detail()"/>

I presume you wanted each item on a new line hence the <p> tags. You can't put HTML markup in an alert box but you can use \\n .

function detail(){
    var txt="";
    txt ="Browser CodeName: "+ navigator.appCodeName +"\n";
    txt +="Browser Name: "+ navigator.appName +"\n";
    txt +="Browser Version: "+ navigator.appVersion +"\n";
    txt +="Cookies Enabled: "+ navigator.cookieEnable +"\n";
    txt +="Browsr Language: "+ navigator.language +"\n";
    txt +="Browser Online: "+ navigator.onLine+"\n";
    txt +="Platform: "+ navigator.platform +"\n";
    txt +="User-agent header: "+ navigator.userAgent +"\n";
    txt +="User-agent language: "+ navigator.systemLanguage +"\n";

    alert(txt);
}

http://jsfiddle.net/Me3xq/

Did a quick research not sure if this can meet your requirement.. You can use Unicode characters and the escape characters \\n and \\t. An example:

alert( 
    'This is an alert with basic formatting\n\n'
    + "\t• list item 1\n"
    + '\t• list item 2\n'
    + '\t• list item 3\n\n'
    + 'Simple table\n\n'
    + 'Char\t| Result\n'
    + '\\n\t| line break\n'
    + '\\t\t| tab space'
);

or if you want to use something more perfect use the modal div popup .. here is the link

alert(message)

message -- Specifies the text to display in the alert box, or an object converted into a string and displayed

So the coming result is correct. You can't write HTML code in alert.

If you want to write it to DOM use:

document.getElementsByTagName('body')[0].innerHTML=(txt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM