www.webdeveloper.com
Results 1 to 10 of 10

Thread: Browser Detection and functioning - Error in code. HELP

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    5

    Exclamation Browser Detection and functioning - Error in code. HELP

    Hi there,

    I need someone to check my JavaScripting Code for me. It's being used in html and I use Dreamweaver to edit it.
    Coding Background:
    This code is supposed to determine which browser a user is using, and with that then determined, it should run BrowserType1() or BrowserType2(), hereon spoken of as fnA() or fnB(). Those functions have different html titles, therefore it should work seamlessly. The issue comes in at Ln. 317. For some reason, it doesn't want to accept my else if... code.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <!-- (C) 2013 Johan Brink - ALL RIGHTS RESERVED  (Pandora Information Systems)
    	 Johan.Brink1@telkomsa.net
         		FOR
         RADIO STRAND (PTY.) LTD.
    -->
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <script type="text/javascript">
    <!--
    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));
    	},
    	
    <!-- Browsers that could be detected:
    <!-- *  Chrome
    <!-- *  OmniWeb
    <!-- *  Safari
    <!-- *  Opera
    <!-- *  iCab
    <!-- *  Konqueror
    <!-- *  Firefox
    <!-- *  Camino
    <!-- *  Netscape
    <!-- *  MSIE
    <!-- *  Mozilla
    
    	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",
    			versionSearch: "Version"
    		},
    		{
    			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.userAgent,
    			   subString: "iPhone",
    			   identity: "iPhone/iPod"
    	    },
    		{
    			string: navigator.platform,
    			subString: "Linux",
    			identity: "Linux"
    		}
    	]
    
    };
    BrowserDetect.init();
    
    // -->
    </script>
    <script type="text/javascript">
    <!-- This is the title to display if Browser is Mozilla or Firefox
    function BrowserType1()
    {
    	var message = new Array();
    	message[0] = " Browser Page Title 1 ";
    
    	var reps = 2;
    	var speed = 200;  
    	var p = message.length;
    	var T = "";
    	var C = 0;
    	var mC = 0;
    	var s = 0;
    	var sT = null;
    	var cln_ = ":"
    	<!-- These are Music Notes, as per company's request
    	var nt1 = String.fromCharCode(9833)
    	var nt2 = String.fromCharCode(9834)
    	var nt3 = String.fromCharCode(9835)
    	var nt4 = String.fromCharCode(9836)
    	var nt5 = String.fromCharCode(9837)
    	var nt6 = String.fromCharCode(9838)
    	var nt7 = String.fromCharCode(9839)
    	if (reps < 1) reps = 1;
    		function doTheThing() 
    	{
    		T = message[mC];
    		str1 = nt1+cln_+cln_+cln_+cln_+cln_+cln_  //nt1::::::
    		str2 = cln_+nt2+cln_+cln_+cln_+cln_+cln_  //:nt2:::::
    		str3 = cln_+cln_+nt3+cln_+cln_+cln_+cln_  //::nt3::::
    		str4 = cln_+cln_+cln_+nt4+cln_+cln_+cln_  //:::nt4:::
    		str5 = cln_+cln_+cln_+cln_+nt5+cln_+cln_  //::::nt5::
    		str6 = cln_+cln_+cln_+cln_+cln_+nt6+cln_  //:::::nt6:
    		str7 = cln_+cln_+cln_+cln_+cln_+cln_+nt7  //::::::nt7
    		A();
    	}
    	function A() 
    	{
    		s++;
    		if (s > 12) { s = 1;}
    		// 
    		if (s == 1) { document.title = str1+' '+T+' '+str7 ; }
    		if (s == 2) { document.title = str2+' '+T+' '+str6 ; }
    		if (s == 3) { document.title = str3+' '+T+' '+str5 ; }
    		if (s == 4) { document.title = str4+' '+T+' '+str4 ; }
    		if (s == 5) { document.title = str5+' '+T+' '+str3 ; }
    		if (s == 6) { document.title = str6+' '+T+' '+str2 ; }
    		if (s == 7) { document.title = str7+' '+T+' '+str1 ; }
    		if (s == 8) { document.title = str6+' '+T+' '+str2 ; }
    		if (s == 9) { document.title = str5+' '+T+' '+str3 ; }
    		if (s == 10) { document.title = str4+' '+T+' '+str4 ; }
    		if (s == 11) { document.title = str3+' '+T+' '+str5 ; }
    		if (s == 12) { document.title = str2+' '+T+' '+str6 ; }
    		if (s == 13) { document.title = str1+' '+T+' '+str7 ; }
    		if (C < (12 * reps)) 
    		{
    			sT = setTimeout("A()", speed);
    			C++;
    		}
    	
    		else 
    		{
    			C = 0;
    			s = 0;
    			mC++;
    			if(mC > p - 1) mC = 0;
    			sT = null;
    			doTheThing();
    		}
    	}
    	
    	doTheThing();
    	//  End -->
    }
    
    <!-- This is the title to display if browser is Internet Explorer or anything else
    function BrowserType2()
    {
    	var message = new Array();
    	message[0] = " Browser Page Title ";
    	var reps = 2;
    	var speed = 200;  
    	var p = message.length;
    	var T = "";
    	var C = 0;
    	var mC = 0;
    	var s = 0;
    	var sT = null;
    	var cln_ = ":"
    	var nt1 = "#"  // "Sharp" for music notation
    
    	if (reps < 1) reps = 1;
    	function doTheThing() 
    	{
    		T = message[mC];
    		str1 = nt1+cln_+cln_+cln_+cln_+cln_+cln_  //#::::::
    		str2 = cln_+nt1+cln_+cln_+cln_+cln_+cln_  //:#:::::
    		str3 = cln_+cln_+nt1+cln_+cln_+cln_+cln_  //::#::::
    		str4 = cln_+cln_+cln_+nt1+cln_+cln_+cln_  //:::#:::
    		str5 = cln_+cln_+cln_+cln_+nt1+cln_+cln_  //::::#::
    		str6 = cln_+cln_+cln_+cln_+cln_+nt1+cln_  //:::::#:
    		str7 = cln_+cln_+cln_+cln_+cln_+cln_+nt1  //::::::#
    
    		A();
    	}
    	
    	function A() 
    	{
    		s++;
    		if (s > 12) { s = 1;}
    		// 
    		if (s == 1) { document.title = str1+' '+T+' '+str7 ; }
    		if (s == 2) { document.title = str2+' '+T+' '+str6 ; }
    		if (s == 3) { document.title = str3+' '+T+' '+str5 ; }
    		if (s == 4) { document.title = str4+' '+T+' '+str4 ; }
    		if (s == 5) { document.title = str5+' '+T+' '+str3 ; }
    		if (s == 6) { document.title = str6+' '+T+' '+str2 ; }
    		if (s == 7) { document.title = str7+' '+T+' '+str1 ; }
    		if (s == 8) { document.title = str6+' '+T+' '+str2 ; }
    		if (s == 9) { document.title = str5+' '+T+' '+str3 ; }
    		if (s == 10) { document.title = str4+' '+T+' '+str4 ; }
    		if (s == 11) { document.title = str3+' '+T+' '+str5 ; }
    		if (s == 12) { document.title = str2+' '+T+' '+str6 ; }
    		if (s == 13) { document.title = str1+' '+T+' '+str7 ; }
    		if (C < (12 * reps)) 
    		{
    			sT = setTimeout("A()", speed);
    			C++;
    		}
    		else 
    		{
    			C = 0;
    			s = 0;
    			mC++;
    			if(mC > p - 1) mC = 0;
    				sT = null;
    				doTheThing();
    		}
    	}
    	doTheThing();
    	//  End -->
    }
    <!-- Here we detect the browser to determine which title fn will be used
    if (BrowserDetect.browser = "Mozilla");
    {
    	BrowserType1();
    }
    
    else if (BrowserDetect.browser = "Firefox");
    {
    	BrowserType1();
    }
    
    else
    {
    	BrowserType2();
    }
    </script>
    
    
    </head>
    
    <body>
    <h1>Hello</h1>
    
    <script type="text/javascript">
    <!--
    document.write('<p class="accent">You\'re using ' + BrowserDetect.browser + ' ' + BrowserDetect.version + ' on ' + BrowserDetect.OS + '!</p>');
    // -->
    </script>
    </body>
    </html>

    Thanks,
    Johan Brink
    Pandora Information Systems

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    This is the line it doesn't like.

    Code:
    else if (BrowserDetect.browser = "Firefox");
    Hmmm
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Mar 2009
    Posts
    501
    In both

    if (BrowserDetect.browser = "Mozilla"); and
    else if (BrowserDetect.browser = "Firefox");

    you need to being using '==' or '===', NOT '='

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by Tcobb View Post
    In both

    if (BrowserDetect.browser = "Mozilla"); and
    else if (BrowserDetect.browser = "Firefox");

    you need to being using '==' or '===', NOT '='
    That's one of the errors. People have to learn to debug for themselves.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  5. #5
    Join Date
    Jan 2013
    Posts
    5
    Even if I use == or ===, it gives me a debugging error. I've tried literally e-v-e-r-y-t-h-i-n-g, and I can't find the problem with it to actually know what solution to use there. It's like it doesn't accept 'else if...' statements even though it does

  6. #6
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by PandoraIS View Post
    Even if I use == or ===, it gives me a debugging error. I've tried literally e-v-e-r-y-t-h-i-n-g, and I can't find the problem with it to actually know what solution to use there. It's like it doesn't accept 'else if...' statements even though it does
    Then there must be a syntax error. All you have to do is find it and fix it.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  7. #7
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    You have semicolons right after those if-statements.
    New to web development or in need of a good reference? Check out the Mozilla Developer Network or W3Schools.

  8. #8
    Join Date
    Jan 2013
    Posts
    5
    Thanks, Tcobb and refreezed!

    Your help is greatly appreciated! Code is working (almost) fine now. Now, there's a new problem. You know fnA and fnB (mentioned in initial post) as defined in the code and used in the if-statements - Those are used to set a multi-title for the webpage. Calling up the functions in the if...else if...else... statements, they render as static text, and not as per scripted at the definition of the functions. If you don't understand what I just said, just run each function separately and you'll know what I'm talking about. Any solutions?


    last but not least, thanks Logic Ali, you will not be forgotten.

  9. #9
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by refreezed View Post
    You have semicolons right after those if-statements.
    You have just permanently weakened the OP's chances of becoming a proficient programmer.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  10. #10
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    Quote Originally Posted by Logic Ali View Post
    You have just permanently weakened the OP's chances of becoming a proficient programmer.
    He might just have overlooked those small mistakes. It's not like he was putting semicolons after every if-statement...

    I'll end this post with that smart thing someone said at some point... "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." :|
    New to web development or in need of a good reference? Check out the Mozilla Developer Network or W3Schools.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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