Jump to content

Broswer detection not working properly.....


unplugged_web
 Share

Recommended Posts

I've got a JavaScript file to detect someone's browser and then load a particular css file depending on which browser they have. It works perfectly (I've used <script type="text/javascript" src="extra/js/browser.js"></script> to load it into the page) until I attach it to a PHP page. When it's attached to a PHP page I get an error saying 'd.write is not a function'. What does this mean and what can I do to change it and make it work with PHP pages as well?The code in the JavaScript file is:

var d, dom, ie, ie4, ie5x, moz, mac, win, lin, old, ie5mac, ie5xwin, op;d = document;n = navigator;na = n.appVersion;nua = n.userAgent;win = ( na.indexOf( 'Win' ) != -1 );mac = ( na.indexOf( 'Mac' ) != -1 );lin = ( nua.indexOf( 'Linux' ) != -1 );if ( !d.layers ){	dom = ( d.getElementById );	op = ( nua.indexOf( 'Opera' ) != -1 );	konq = ( nua.indexOf( 'Konqueror' ) != -1 );	saf = ( nua.indexOf( 'Safari' ) != -1 );	moz = ( nua.indexOf( 'Gecko' ) != -1 && !saf && !konq);	ie = ( d.all && !op );	ie4 = ( ie && !dom );	/*	ie5x tests only for functionality. ( dom||ie5x ) would be default settings. 	Opera will register true in this test if set to identify as IE 5	*/	ie5x = ( d.all && dom );	ie5mac = ( mac && ie5x );	ie5xwin = ( win && ie5x );	if ( moz ){		d.write('<link rel = "stylesheet" type = "text\/css" href = "extra/css/static/news-panel.css" />');	}	else if ( op ){		d.write('<link rel = "stylesheet" type = "text\/css" href = "extra/css/static/news-panel.css" />');	}	else if ( saf ){		d.write('<link rel = "stylesheet" type = "text\/css" href = "extra/css/static/news-panel.css" />');	}	else if ( konq ){		d.write('<link rel = "stylesheet" type = "text\/css" href = "extra/css/static/news-panel.css" />');	}	else if ( ie5mac ) {		d.write('<link rel = "stylesheet" type = "text\/css" href = "extra/css/static/news-panel.css" />');	}	else	{		d.write('<link rel = "stylesheet" type = "text\/css" href = "extra/css/static/news-panel_ie.css" />');	}}

Thanks

Link to comment
Share on other sites

look at the script. d.write is not the correct function to do this on. have you looked at w3schools.com? if by d.write you mean "document.write() then try typing out the function and try that on. if it currently does not work after that then let reply

Link to comment
Share on other sites

look at the script. d.write is not the correct function to do this on.
The first line:d = document;Where are you putting the script tag? In order for document.write to work it needs to be in the body, not the head. If it's in the head then you can't use document.write. It's also not correct to write a link tag in the body, so you need to replace the calls to document.write with code that will add a stylesheet to the page. There is a stylesheets collection in the DOM that you can use for this, do a search on how to use Javascript to add a stylesheet and you'll see how.
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
 Share

×
×
  • Create New...