dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: Redirect based on browser

  1. #1
    Join Date
    Mar 2003
    Posts
    447

    Redirect based on browser

    Howdy yall~
    I have a dumb question: I have a code that redirects the user based on their browser and I have it working for IE, Firefox and Chrome but I can not figure out how to make it work any other browsers like Microsoft Edge. Can someone help me out? Great the script was working yesterday for all the browsers now none of them work, what the heck did I do?

    Code:
    <script type="text/javascript">
        function detectBrowser(){
            var nAgent = navigator.userAgent;
            var verOffset;
    
                if ((nAgent.indexOf("MSIE"))!=-1) {
                 browserName = "Microsoft Internet Explorer";
                  window.location = "msiebrowser.html";
                }
                else if ((verOffset=nAgent.indexOf("Chrome"))!=-1) {
                 browserName = "Chrome";
                    window.location = "cbrowswe.html";
                }
                else if ((verOffset=nAgent.indexOf("Firefox"))!=-1) {
                 browserName = "Firefox";
                     window.location = "ffbrowser.html";
                }
        }
    </script>
    Thanks Taz
    Last edited by Tasmanian Devil; 01-29-2016 at 02:21 PM.
    "It is better to keep your mouth shut and look like a fool, rather than open your mouth and be a fool!!!"

    "It is better to say nothing, and be thought a fool, rather than open your mouth and confirm it!!!"

  2. #2
    Join Date
    Oct 2013
    Posts
    1,445
    First, Why?? What is so different about those pages that they need to be displayed based on the browser?

    Second, IE >= 10 doesn't ID itself as Internet Explorer (MSIE), so that breaks right away.

    What about Safari?

    What about mobile?

    What about users who choose to not have Javascript running?

    Lastly, and this is a workaround, if you really need different pages for different browsers create an index.xxx with links to those browser-specific pages. For example:
    Code:
    I am using <a href="ffbrowser.html">Firefox</a>.
    That allows the user to choose their own poison.
    In dog beers I've only had one.

  3. #3
    Join Date
    Mar 2003
    Posts
    447
    Kevin~
    First of all the so much javascript to make things look good on the page and depending what browser they use some of the javascripts work differently in the different browsers, i.e. calendar of events script that I want to use works in all browsers but chrome so I just figured out how to link different pages that work in each browser together.

    I figured that if I had a code in my index page that automatically redirects the user instead of makin em pick a link only if the javascript is not running.

    What do ya think would be the best since ya kinda know what I am trying to achieve

    Thanks
    Taz
    "It is better to keep your mouth shut and look like a fool, rather than open your mouth and be a fool!!!"

    "It is better to say nothing, and be thought a fool, rather than open your mouth and confirm it!!!"

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    3,624
    TBH, pointless because some browsers can be set to masquerade as a different browser.

    Use CSS and conditional elements.
    --> JavaScript Frameworks like JQuery, Angular, Node <-- ... and please remember to wrap any code you have in forum tags:-
    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    Nov 2010
    Posts
    1,276
    The first thing to do would be to figure out why it is not working in Edge. Is it a javascript problem (have a look at the error console) or CSS drama?

    I think both are somewhat unlikely as Edge is far more standardised than any other IE product but they have probably still left some legacy junk in there just to trip you up.

    Back in the day when browsers were radically different we would use feature detection, not to figure out which browser it was (there are new browsers coming on the market every day, so getting into that hooha will just involve an endless game of catch-up) but to decide which code could be used with the browser in use at the time. This is the only sane approach. As the other two posters have noted, browser sniffing like what you are doing is pointless because you can never trust the results.

    The other thing you want to avoid is having multiple builds of the same page and having to update all of them when you want to make one tweak. Just a recipe for disaster.

    CSS hacks are more reliable but if you think carefully about what you're doing and aren't trying to use anything too hey-hoo-whoopdy-doo are largely unneccesary - most of the CSS hacks out there were implemented for older browsers or by lazy developers.

    Further, "so much javascript to make things look good on the page" is a worrying statement because making things look good is not javascript's job at all really - all that should be done in CSS

    Additionally, if the calendar script is not working in Chrome you REALLY need to find out why, as Chrome is one of the most compliant when it comes to standards and the problem here can only really be either a bum or antiquated script or a poor implementation.

    If it turns out to be the first, you would have the option of fixing it but the reality is that there are a million (I haven't counted them, but let's say) calendar scripts out there. It's quite possible that the easiest solution would be just to find a calendar script that works across browsers and on mobile as well. Any plugin developer worth their name in 2016 is developing with mobile in mind if not as priority number 1

  6. #6
    Join Date
    Oct 2013
    Posts
    1,445
    Not much to add to the previous, highly esteemed (and I mean that), respondents. Look, valid HTML and valid CSS should look pretty close to the same cross-browser. It's possible that a CSS rule or three would benefit from a -webkit- vendor prefix to make things look better in Chrome. See shouldiprefix.com, although that list gets smaller seemingly weekly.
    In dog beers I've only had one.

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