www.webdeveloper.com
Results 1 to 6 of 6

Thread: JS browser detect & page redirect

  1. #1
    Join Date
    Jan 2009
    Posts
    3

    JS browser detect & page redirect

    I'm trying to have a Javascript that detects the visitor's browser, and if he/she is using a specific browser, redirects the visitor to a different page (for compatibility purposes).

    I got this code from several websites and put them together, but they don't really work. Anyone willing to help?

    Thanks in advance.

    Code:
    var BrowserDetect = {
    	init: function () {
    		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    		this.version = this.searchVersion(navigator.userAgent)
    			|| this.searchVersion(navigator.appVersion)
    			|| "an unknown version";
    		this.OS = this.searchString(this.dataOS) || "an unknown OS";
    	},
    	searchString: function (data) {
    		for (var i=0;i<data.length;i++)	{
    			var dataString = data[i].string;
    			var dataProp = data[i].prop;
    			this.versionSearchString = data[i].versionSearch || data[i].identity;
    			if (dataString) {
    				if (dataString.indexOf(data[i].subString) != -1)
    					return data[i].identity;
    			}
    			else if (dataProp)
    				return data[i].identity;
    		}
    	},
    	searchVersion: function (dataString) {
    		var index = dataString.indexOf(this.versionSearchString);
    		if (index == -1) return;
    		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    	},
    	dataBrowser: [
    		{
    			string: navigator.userAgent,
    			subString: "Chrome",
    			identity: "Chrome"
    		},
    		{ 	string: navigator.userAgent,
    			subString: "OmniWeb",
    			versionSearch: "OmniWeb/",
    			identity: "OmniWeb"
    		},
    		{
    			string: navigator.vendor,
    			subString: "Apple",
    			identity: "Safari",
    			versionSearch: "Version"
    		},
    		{
    			prop: window.opera,
    			identity: "Opera"
    		},
    		{
    			string: navigator.vendor,
    			subString: "iCab",
    			identity: "iCab"
    		},
    		{
    			string: navigator.vendor,
    			subString: "KDE",
    			identity: "Konqueror"
    		},
    		{
    			string: navigator.userAgent,
    			subString: "Firefox",
    			identity: "Firefox"
    		},
    		{
    			string: navigator.vendor,
    			subString: "Camino",
    			identity: "Camino"
    		},
    		{		// for newer Netscapes (6+)
    			string: navigator.userAgent,
    			subString: "Netscape",
    			identity: "Netscape"
    		},
    		{
    			string: navigator.userAgent,
    			subString: "MSIE",
    			identity: "Explorer",
    			versionSearch: "MSIE"
    		},
    		{
    			string: navigator.userAgent,
    			subString: "Gecko",
    			identity: "Mozilla",
    			versionSearch: "rv"
    		},
    		{ 		// for older Netscapes (4-)
    			string: navigator.userAgent,
    			subString: "Mozilla",
    			identity: "Netscape",
    			versionSearch: "Mozilla"
    		}
    	],
    	dataOS : [
    		{
    			string: navigator.platform,
    			subString: "Win",
    			identity: "Windows"
    		},
    		{
    			string: navigator.platform,
    			subString: "Mac",
    			identity: "Mac"
    		},
    		{
    			string: navigator.platform,
    			subString: "Linux",
    			identity: "Linux"
    		}
    	]
    
    };
    BrowserDetect.init();
    
    if (dataBrowser: [{identity: "Safari"}]) function move() { window.location = 'http://www.thefreaks.co.cc/index2.html' } else 
    
    if (dataBrowser: [{identity: "Firefox"}]) function move() { window.location = 'http://www.thefreaks.co.cc/index2.html' } else

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    i doubt any seasoned webdevs are going to be too excited about using redirects for compatibility.

    try to make one page that works in all browsers.

    it can definitely be done.

  3. #3
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Your code also doesn't take into account the other 1000+ different browsers and the fact that the useragent can be set to anything in at least some browsers (eg. IE, Firefox).

  4. #4
    Join Date
    Jun 2004
    Posts
    316
    Actually, it is possible to create "Stunning" websites if you code browser-specifically, and browser checking/redirect is a good way to do that (worse comes to worse, the code inside can be blocked out using <!-- and --> for extra compatibility, and an "all-browser-compatibility" page can be displayed if the browser-check fails).

  5. #5
    Join Date
    Jan 2009
    Posts
    3
    @felgall: I didn't create this code, I just took some bits from different websites and put them together :-/ The main purpose of this code is just to redirect users of two specific browsers to a different webpage, while all other browsers see the same webpage.

    I was expecting help

  6. #6
    Join Date
    Jun 2004
    Posts
    316
    I have edited this code that I have found on the internet for you:

    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    <!--
    function detectBrowser()
    {
    var browser=navigator.appName;
    var b_version=navigator.appVersion;
    var version=parseFloat(b_version);
    if ((browser=="Netscape"||browser=="Microsoft Internet Explorer")
    && (version>=4))
    {
    if(browser=="Netscape") { location.replace("http://www.test.com/firefox.htm"); }
    else { location.replace("http://www.test.com/internetexplorer.htm"); }
    }
    
    } // -->
    </script>
    </head>
    
    <body onload="detectBrowser()">
    This will be displayed if your browser is NOT Internet Explorer/Firefox
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles