Jump to content

Browser redirect


Jonas

Recommended Posts

Ok, so I've used simple scripts for some time, and now I'm making a site for my dad's company. However, due to Internet Explorer and Mozilla Firefox's showing css differently from the browser I'm using (Opera), I'm not managing to show the site the way I want.http://www.w3schools.com/js/tryit.asp?file...js_crossbrowserSo, after reading that, and also having searched around a bit on the web, is there any way of redirecting to different browsers. My biggest problem so far is that Opera disguises itself as Internet Explorer when "navigator.appname" is called through a function. Also, I found this script:http://www.cssplay.co.uk/archive/themes_v2.json this site:http://www.cssplay.co.uk/and it gave me the idea of just switching stylesheet based on browser. I managed to make a script where you could choose stylesheet through <select> and <option>. But that doesn't work in IE for some reason, and besides I don't want to have the reader change stylesheet for every new link or update. Like I said, ideally the ideal stylesheet should be chosen based on browser. SO, is there anyway of doing this? Any ideas? Could navigator.userAgent be an idea? Can anyone good at javascript help me with this?Any help highly appreciated... :)

Link to comment
Share on other sites

You could send the user to a different page depending on their browser....each page with a different style sheet. Using window.location. And about opera disguising itself; i think that there are more intricate navigator.x functions that will return the fact that it's opera. Yes, i know, i really stink. :)

Link to comment
Share on other sites

userAgent is a nicer one, Opera declares itself through that. A combination of appName and userAgent should provide you with enough to browser sniff. It's always good to check whether the function you're trying to use is directly supported though - i.e:if( document.thisThing() ){}

Link to comment
Share on other sites

Ok, so it wasn't that hard after all... :)

  <script type="text/javascript"> 	 var useragent = navigator.userAgent; 	 var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName; 	 if (bName == "Opera") {    document.write('<link type="text/css" rel="stylesheet" href="opera.css" />') 	 } 	 else if (bName == "Netscape") {    document.write('<link type="text/css" rel="stylesheet" href="netscape.css" />') 	 } 	 else {    document.write('<link type="text/css" rel="stylesheet" href="microsoft.css" />') 	 }  </script>

Link to comment
Share on other sites

Ok, so it wasn't that hard after all... :(
  <script type="text/javascript">  	var useragent = navigator.userAgent;  	var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;  	if (bName == "Opera") {    document.write('<link type="text/css" rel="stylesheet" href="opera.css" />')  	}  	else if (bName == "Netscape") {    document.write('<link type="text/css" rel="stylesheet" href="netscape.css" />')  	}  	else {    document.write('<link type="text/css" rel="stylesheet" href="microsoft.css" />')  	}  </script>

LOL! With this new code I get validation error, saying <link> element is not allowed where it is. :) I just cheated using document.write('<'+'...') :) Now I just get a warning, but still valid xhtml 1.1... :D
Link to comment
Share on other sites

I've had this too. I just end up throwing all javascripts into a .js file, solves it
Yeah, tried that, but it didn't work for some reason. When I tried it (due to my choice of <link etc. />), it didn't write it into my document, and so no stylesheet was loaded. If it was redirect based on browser I suppose it would work, but with the script changing css file, I don't have to have three of each document...
Link to comment
Share on other sites

:)"+-->
QUOTE ("Skin Selector Javascript Code Thing :)")
  <script type="text/javascript">  var useragent = navigator.userAgent;  var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;  if (bName == "Opera") {  document.write('<link type="text/css" rel="stylesheet" href="opera.css" />')  }  else if (bName == "Netscape") {  document.write('<link type="text/css" rel="stylesheet" href="netscape.css" />')  }  else {  document.write('<link type="text/css" rel="stylesheet" href="microsoft.css" />')  } </script>
don't you have to escape the <'s?Also, did you take out the script tag?(for external hostign)
Link to comment
Share on other sites

:)"+-->
QUOTE ("Skin Selector Javascript Code Thing :)")
  <script type="text/javascript">  var useragent = navigator.userAgent;  var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;  if (bName == "Opera") {   document.write('<link type="text/css" rel="stylesheet" href="opera.css" />')  }  else if (bName == "Netscape") {   document.write('<link type="text/css" rel="stylesheet" href="netscape.css" />')  }  else {   document.write('<link type="text/css" rel="stylesheet" href="microsoft.css" />')  } </script>
don't you have to escape the <'s?Also, did you take out the script tag?(for external hostign)

I got a validation warning that I should escape the "<" with < yes, but if I do that it's not implemented as a tag like I want it to... :DAnd yes, removed the script tag. Give me some credit here... :(Dunno why it doesn't work... :)
Link to comment
Share on other sites

<script type="text/javascript">  var useragent = navigator.userAgent;  var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;  if (bName == "Opera") {  document.write('<link type=\"text\/css\" rel=\"stylesheet\" href=\"opera.css\" />')  }  else if (bName == "Netscape") {  document.write('<link type=\"text\/css\" rel=\"stylesheet\" href=\"netscape.css\" />')  }  else {  document.write('<link type=\"text\/css\" rel=\"stylesheet\" href=\"microsoft.css\" />')  }</script>

there ya go.. remember to backslash your quatation marks, forwardslahses, e.t.c.

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...