Jump to content

Displaying Javascript Result


Err

Recommended Posts

Hello again, I'm having a small problem with javascript here. I found this really neat browser detection script and would like to use it in a cell in one of my webpages, problem is I need to display the full javascript code in that cell in order for it to work, to say the least it would look less then great as I'm also trying to stay xhtml standard compliant, I've already tried to link the javascript file extrenally to that cell, but it doesn't work :) . I was wondering if some javascript guru can help me display the javascript result in a cell without the need to add the whole code to the cell. If I could I would do this myself, but I can't :)something like:==javascript here=== |¯¯¯¯¯¯¯¯¯¯¯¯||result here ||____________|I hope it's not too much to askhere is the code, any help I can get on this would be great!

var detect = navigator.userAgent.toLowerCase();var OS, browser, version, thestring, place;if (checkIt('konqueror')) {browser = "Konqueror";OS = "Linux";} else if (checkIt('safari')) browser = "Safari";else if (checkIt('omniweb')) browser = "OmniWeb";else if (checkIt('k-meleon')) {browser = "K-Meleon";var rv = detect.match(/k-meleon ([\w.]+)/);if (rv) {rv = rv[0];version = rv.substr(3);}}else if (checkIt('opera')) browser = "Opera";else if (checkIt('webtv')) browser = "WebTV";else if (checkIt('icab')) browser = "iCab"else if (checkIt('msie')) browser = "Internet Explorer";else if (!checkIt('compatible')) {browser = "Netscape Navigator"version = detect.charAt(8);if (typeof(navigator.product) != "undefined") {browser = "Mozilla";rv = navigator.userAgent.match(/([Mozilla ]?Fire\w+)\/([\w|\+.]+)/);if (rv) {browser += rv[1];version = rv[2];} else {rv = navigator.userAgent.match(/rv:([\w|\+.]+)/);if (rv) {rv = rv[0];version = rv.substr(3);}}}}else browser = "An unknown browser";if (!version) {version = detect.charAt(place + thestring.length);}var browsername = "";if (browser)browsername += browser;if (version)browsername += " " + version;(browsername) ? document.writeln(browsername) : document.writeln("Unable to detect!");
Link to comment
Share on other sites

just wrap the pretty little javascript up in a function (make sure you return the value you want to use). Change the document.writeln at the end to a return statement.Then make another function and call it on the body element's onload event. That function will call the other one to get the browser info you want to display, then use the HTML DOM to print it to the location you want. Give that location an id like "browserCell".Didn't feel like test so there might be something missing/wrong...

function writeBrowserInfo{    browserInfo = getBrowserInfo();    document.getElementById("browserCell").innerHTML += browserInfo;}

Instead of using to methods, you could just as easily use one, and change the document.writeln to do the same as what I put in writeBrowserInfo().

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...