SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How to link to diference js file base on specific target browser

    Hi,
    I have stupid question.
    I'm trying to pull a specific js file which matches specific target browser like something below:
    Code:
    if browser = webkit
         <script src='webkit.js'></script>
    else if browser = opera
         <script src='opera.js'></script>
    Can I achieve this. How so?

    Thanks in advance

  2. #2
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    On the side note: I don't really want to target different browsers, just the feature associate with them.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    So test for the features you need to distinguish on. JavaScript has no way to tell which browser is which apart from which features it supports.
    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="^$">

  4. #4
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks for reply,

    base on my research, document.write("<script src='opera.js'></script>"); should work.

  5. #5
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,197
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by ketting00 View Post
    Thanks for reply,

    base on my research, document.write("<script src='opera.js'></script>"); should work.
    But as felgall said, you don't know when to use that because you don't know which browser someone is using. In JS, you can test that a feature is supported before actually using it ... which is what felgall was saying.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ketting00 View Post
    Thanks for reply,

    base on my research, document.write("<script src='opera.js'></script>"); should work.
    Apart from the fact that you will never know when to call that because you can't tell whether the browser is Opera or not unless your visitor tells you it is (in which case it might still be Internet Explorer or Firefox pretending to be Opera) the </script> there will terminate the script - you would need to specify it as <\/script> for it to work.

    Also document.write became obsolete when Netscape 4 died. There are several better ways to generate HTML from JavaScript now.
    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="^$">

  7. #7
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Also document.write became obsolete when Netscape 4 died. There are several better ways to generate HTML from JavaScript now.
    I think this going to be giving me a headache.

  8. #8
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,197
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    What's your aim with this, anyhow? It doesn't sound very practical to target browsers in general.

  9. #9
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks to make it clear. I'll keep that in mind.

  10. #10
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    325
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This is stupid considering this post: http://www.sitepoint.com/ie7-tax/


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
  •