SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Browser detectionm

    I have got this code and it dosnt work i just get javascript error: i its suppost to output that code only in ie:

    Code:
    <script language="JavaScript"
    type="text/JavaScript">
     if(navigator.appName == "Microsoft Internet Explorer")
    {
                      <object id="ChatFrame" classid="CLSID:F58E1CEF-A068-4c15-BA5E-587CAF3EE8C6" width="100%" height="100%" codebase="/controls/msnchat45.cab#Version=9.00.0305.1401">
    				    <param name="RoomName" value= "<?= $room ?>">
    				      <param name="HexRoomName" value= "<?= $rhx ?>">
    				        <param name="UserRole" value= "<?= $perms ?> <?= $username ?> <?= $qry[normalpass] ?>">
    				                    <param name="NickName" value= "<?= $num ?>">
    				        <param name="Server" value="ninechat.ath.cx">
    				    <param name="BaseURL" value="http://servers2/newsite/">
    				    <param name="ChannelLanguage" value="EN-US">
    				    <param name="MessageOfTheDay" value="<? include 'C:/New-Chat-Server/motd.txt' ?>">
    				    <param name="CreationModes" value="">
    				    <param name="ChatMode" value="0">
    				    <param name="Feature" value="0">
    				    <param name="Category" value="">
    				    <param name="Topic" value="<?= $topic ?>">
    				    <param name="WelcomeMsg" value="<?= $welcome ?>">
    				    <param name="Locale" value = "EN-GB">
    				    <param name="ResDLL" value = "/controls/msnchat40en-us.cab#Version=9,02,0310,2401">
    				      <param name="ButtonTextColor" value="0">
    				    <param name="ButtonBackColor" value="16180173">
    				    <param name="ButtonFrameColor" value="16750949">
    				    <param name="BackColor" value="16777215">
    				    <param name="InputBorderColor" value="9915136">
    				    <param name="BackHighlightColor" value="16248815">
    				    <param name="TopBackHighlightColor" value="10768896">
                      </object>
    }
    </script>
    Animated Chatrooms - www.121chatrooms.net

  2. #2
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Browser detection is not an easy thing to implement.

    Download and use a JavaScript framework (i.e. JQuery).

  3. #3
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with Remiya, using a JS library would be best...

    Although it might be better to try out a more lightweight detection solution...
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  4. #4
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lightweight solutions are good, but the libraries have more support.

  5. #5
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but if he's just looking for a browser-detection script then Koch's script might be more suitable.

    If all you're going to use a library for is browser-detection then it's a waste of bandwidth... (for the users)
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  6. #6
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JimmyP View Post
    Yes, but if he's just looking for a browser-detection script then Koch's script might be more suitable.
    Totally agree with you. However I prefer libraries, because it is not only browser detection I do on one page.

  7. #7
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason you're getting a JavaScript error is because you've put HTML markup in the if statement. You can't intersperse HTML markup with JavaScript code the way you can do it with PHP or ASP. If you want to add an element with JavaScript you have three options:
    1. Use DOM methods like document.createElement()
    2. Use the non-standard .innerHTML property
    3. Use document.write() if the script is executed in-place
    Birnam wood is come to Dunsinane

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you write your JavaScript properly then it doesn't matter what browser is being used. If the JavaScript only works in some browsers then that indicates an error in the way you have coded the JavaScript.

    It is not a reason for using browser detection - which should only ever be used if you are trying to capture statistics on who is using which browser since it is impossible to correctly identify a browser 100&#37; of the time from within JavaScript unless the browser is Internet Explorer (using JScript conditional comments to identify it) or Opera (if window.opera exists).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    The reason you're getting a JavaScript error is because you've put HTML markup in the if statement. You can't intersperse HTML markup with JavaScript code the way you can do it with PHP or ASP. If you want to add an element with JavaScript you have three options:
    1. Use DOM methods like document.createElement()
    2. Use the non-standard .innerHTML property
    3. Use document.write() if the script is executed in-place
    It may be non-standard, but so are these:

    location, screen, setInterval, setTimeout, escape, unescape, offsetLeft, offsetTop, frames, navigator yet we're forced to use them. I still don't see why innerHTML is that horrible to use, considering it can be much faster than native DOM methods and much, much easier to type.

  10. #10
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo View Post
    The reason you're getting a JavaScript error is because you've put HTML markup in the if statement.
    Yes. Convert your HTML to String.
    Like this

    Code:
    var str = "<div>Hello</div>";
    and write it where needed:

    Code:
    document.write(str);

  11. #11
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA View Post
    Yes. Convert your HTML to String.
    Like this

    Code:
    var str = "<div>Hello</div>";
    and write it where needed:

    Code:
    document.write(str);
    Although I'd use innerHTML or DOM methods over that.

  12. #12
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    Although I'd use innerHTML or DOM methods over that.
    Depends what he wants to achieve, and how he has planned his page.

  13. #13
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA View Post
    Depends what he wants to achieve, and how he has planned his page.
    In no circumstance should you be using document.write(). I suggest you pickup a modern book on DOM Scripting like the one by Jeremy Keith or some of the Sitepoint ones and enlighten yourself before trying to teach others on a topic in which you have a fairly limited knowledge on.

  14. #14
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    In no circumstance should you be using document.write(). I suggest you pickup a modern book on DOM Scripting like the one by Jeremy Keith or some of the Sitepoint ones and enlighten yourself before trying to teach others on a topic in which you have a fairly limited knowledge on.
    Thank you.

  15. #15
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SoulScratch View Post
    I still don't see why innerHTML is that horrible to use
    I didn't say it was horrible, only that it was non-standard.
    Birnam wood is come to Dunsinane

  16. #16
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would use DOM methods here and put all PARAM attributes in an array and then loop through the array and produce the PARAM elements that way...

    Using innerHTML may be faster but it is not standard, meaning that future browsers and future versions of browsers have no obligation to support it.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  17. #17
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JimmyP View Post
    I would use DOM methods here and put all PARAM attributes in an array and then loop through the array and produce the PARAM elements that way...

    Using innerHTML may be faster but it is not standard, meaning that future browsers and future versions of browsers have no obligation to support it.
    User agents have no choice but to support it, same thing with all the DOM 0 properties. Do you really think they'll get rid of location, href, innerHTML, and these properties that existed before there were standards?

    --> http://www.w3.org/TR/html5/dom.html#innerhtml0

  18. #18
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    XHTML has never supported innerHTML so if you want to make the code easier to convert to work with XHTML in the future you should use the DOM.

    innerHTML is also limited in what you can add with it depending on which browser you are using. IE is the most restrictive eg. will not allow you to update parts of tables. Also innerHTML doesn't update the DOM so if you intend to read elements back in through subsequent code you can't use it.

    There is never a need to use document.write in properly written JavaScript for version 5+ browsers that support the DOM.

    location, setInterval, setTimeout, and navigator are a standard part of JavaScript and always have been.

    screen, offsetLeft, offsetTop, etc are part of the BOM (browser object model) which is not defined in the standards and is therefore left for each browser to decide for themselves what to support.

    escape and unescape arte deprecated and should not be used because they are limited in what characters they support. You should use the standard encodeURI and decodeURI commands instead
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •