SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cross-browser best practice?

    As I'm learning Javascript I'm having a problem that LOTS of people here seem to have, based on the postings I've seen in this forum - features working in only one brand of browser, but not another; or working differently in different browsers. Even the examples on many tutorial sites have this problem.

    If I want to limit my use of Javascript to mostly those features that are supported consistently between IE and FF, where can I see a list?

    I know I can query for what browser the client is using and have different code paths, but that sort of conditional coding makes for bloated, messy, hard-to-maintain code, so I'd like minimize it as much as possible and mostly stick to a subset of Javascript that IE and FF agree on whenever possible.

    (In my day job I write C++ for a product that runs on different HW with different compilers so I know how messy platform/compiler -specific coding can be)

    Thanks in advance.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,260
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    cross-browser

    I too avoid browser detection scripts. I think that most problems don't arise from the different browsers' implementation of javascript, but from how they render mark-up and style. If you are using javascript to change the style of text, for example, one browser may consider the text - <p>text</p> - to be a childNode of the p tag, while another would consider it a childNode of a textNode of the p tag. If you used the CSS pseudo-element "child-node" some browsers support it, others don't. Bear in mind that javascript can be disabled so don't use it so that it's absolutely necessary that it be enabled. But if you research the various DOM and CSS differences you should be fairly set. I like the w3schools site. They provide tables that indcate browser support. For example, http://www.w3schools.com/htmldom/dom_obj_text.asp
    and
    http://www.w3schools.com/css/css_reference.asp

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Use feature sensing to test if the browser supports the required feature. For example:

    if (document.images) {
    // browser supports access to images from Javascript
    }

    if (document.getElementById) {
    // browser supports standard DOM
    }
    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
    Join Date
    Aug 2005
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall
    Use feature sensing to test if the browser supports the required feature. For example:

    if (document.images) {
    // browser supports access to images from Javascript
    }

    if (document.getElementById) {
    // browser supports standard DOM
    }
    I thought I was clear that I want to avoid run-time testing whenever possible. Pages with 3 different ways of doing things, depending on what features are supported, results in messy, complex, hard-to-maintain code. I'd rather implement something in just one way by choosing methods that are browser-agnostic, at least for major browsers like FF and IE.

    The other problem with feature testing is that it assumes a boolean conclusion - that a feature is either supported or it isn't. But often the problem is that a feature is supported in different ways or with different behavior on different browsers.

    Thank-you to the other poster for suggesting the DOM and CSS comparison tables - that's exactly the kind of thing I'm looking for. It's remarkable how many different standards are used in even the simplest web pages - HTML. Javascript, DOM, CSS, etc. Combined with user-settings relating to popups, image-display, scripting, cookies and security, it often amazes me that ANY web pages actually work!

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,260
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    Opera

    That reminds me, different browsers render different flavors of HTML differently. And depending on how the doctype is specified, may render in "quirks" mode. Opera has a few tables of what it supports. Here's a link to the supported ECMA page. http://www.opera.com/docs/specs/js/ecma/

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    There is very little Javascript that is compatible across ALL web browsers since Internet Explorer doesn't support Javascript at all and instead supports a similar language called jScript.

    Any access to the web page will require using the DOM which means you need to test if document.getElementById is supported. Any access to the browser (window size, mouse position etc.) will need browser specific code.
    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
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,260
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    javascript support

    Quote Originally Posted by felgall
    Internet Explorer doesn't support Javascript at all
    I have heard this before. I know that Javascript originated with Netscape. I think Microsoft would like it if everyone used VBscript, and I know they came up with Jscript (closer to Javascript in syntax than VBscript). But yet when I view pages in IE that have <script type="text/javascript"> and Javascript code, IE runs it. Just like they do with VBscript and Jscript tags/script. Because I have heard this claim of non-support before I went to the "horse's mouth"
    These pages
    http://msdn.microsoft.com/library/de...erfeatures.asp
    http://msdn.microsoft.com/library/de...erfeatures.asp
    say that javascript is a "supported protocol" and from
    http://msdn.microsoft.com/library/de...CEFeatures.asp
    The browser is capable of rendering documents that contain HTML, Dynamic HTML, and CSS data formats. XML support is available if the MSXML parser is included in the OS design. As a scripting host, Internet Explorer supports Microsoft JScript® 5.6 and Microsoft Visual Basic® Scripting Edition, Microsoft VBScript® 5.6, when these Catalog items are included in an OS design.


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
  •