SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Browser Detection and load

    Hi there

    I'm not much of a developer I know a bit of everything, html, css, php, mysql not much js though. Well I managed to find a
    js menu for a site I'm developing works well. However the position of the menu differs in IE and Firefox. So I've made
    two seperate .js files and tried to create a script to detect what browser the user is using and then load the
    appropriate .js file sounds simple I'm sure it is, but I just can't get it to work.

    Here is my code

    <script type="text/javascript">
    var browserName=navigator.appName;
    alert (browserName)
    if (browserName=="Netscape")
    {
    function Go(){return}</script>
    <script type="text/javascript" src="cat.js"></script>
    <script type="text/javascript" src="menu9_com.js">
    }
    else (browserName=="Microsoft Internet Explorer")
    {
    function Go(){return}</script>
    <script type="text/javascript" src="cat-ie.js"></script>
    <script type="text/javascript" src="menu9_com.js">
    }
    }
    </script>

    What am I doing wrong ?
    Thanks in advance for your help

    Dan

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Script tags close a script-
    You can't start a function in one script and continue with it after the script's end tag.

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,003
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Hi dantheman187, welcome to the forums

    You may have seen PHP code that goes "in and out" of HTML, but PHP is a server-side language and the resulting mark-up doesn't get output until the code has been interpreted.

    Javascript being client-side is different and can be interspersed in the mark-up, but only in complete "blocks" of code, You can set a variable's value inside one and access it another, but As mrhoo said, you can't split functions between script tags.

    Also I suggest you try to avoid relying on "browser detection" as it's highly unreliable. IMHO much better to do "compatability detection".


Bookmarks

Posting Permissions

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