Best way to deal with Javascript (in)compatibility using PHP?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • christian9997@hotmail.com

    Best way to deal with Javascript (in)compatibility using PHP?

    Hi

    We have started off using a $_GET parameter to keep track of the user's
    browser:

    We detect what browser the visitor is using when he first arrives on
    our website then we do a redirect to the same page adding on
    "&browser=I E" or "&browser=D OM" at the end of the url (and we keep the
    browser parameter in the URL the whole time he is on the website), this
    enables us to use the following PHP method:

    function javascript($nam e) {
    if($_GET[brow] == "IE") {
    return "document.all(' $name')";
    }
    else {
    return "document.getEl ementById('$nam e')";
    }
    }

    That we use like this (for example here we are setting the inner HTML
    for the item whose ID is 'contribute'):

    <?=javascript(' contribute')?>. innerHTML = "Name";

    Everything was working fine but we have realised that the Googlebot
    cannot deal with $_GET parameters so we are looking into url rewriting
    but it is going to be very complicated to keep the method we are using
    with url rewriting.

    We would be interested in hearing about the way people deal with
    javascript browser compatibility using PHP. There must be some much
    simpler ways of doing this but we are not sure how to do it.
    Any input would be helpful.

    Thanks

  • Philip Ronan

    #2
    Re: Best way to deal with Javascript (in)compatibili ty using PHP?

    "christian9997@ hotmail.com" wrote:
    [color=blue]
    > Hi
    >
    > We have started off using a $_GET parameter to keep track of the user's
    > browser:[/color]

    Bad idea. Unless you don't really care what happens when people share links
    to your site, or visit your site from search engines.
    [color=blue]
    > We detect what browser the visitor is using when he first arrives on
    > our website then we do a redirect to the same page adding on
    > "&browser=I E" or "&browser=D OM" at the end of the url (and we keep the
    > browser parameter in the URL the whole time he is on the website),[/color]

    That is probably going to damage your search engine ranking.
    [color=blue]
    > this
    > enables us to use the following PHP method:
    >
    > function javascript($nam e) {
    > if($_GET[brow] == "IE") {
    > return "document.all(' $name')";
    > }
    > else {
    > return "document.getEl ementById('$nam e')";
    > }
    > }[/color]

    Has it not occurred to you that you can do all of this in Javascript?

    function getObject(n) {
    if (document.all) return document.all(n) ;
    else return document.getEle mentById(n);
    }

    Here are a couple of links for you:





    --
    phil [dot] ronan @ virgin [dot] net



    Comment

    • Alekc

      #3
      Re: Best way to deal with Javascript (in)compatibili ty using PHP?

      [color=blue]
      > We would be interested in hearing about the way people deal with
      > javascript browser compatibility using PHP. There must be some much
      > simpler ways of doing this but we are not sure how to do it.
      > Any input would be helpful.[/color]

      I'm not sure to understand which is the problem with
      document.getEle mentById for you (it should work on almost all present
      and future browsers) but try to read this page:


      Comment

      • dracolytch

        #4
        Re: Best way to deal with Javascript (in)compatibili ty using PHP?

        Avoid URL rewriting altogether. I think there are more problems
        associated with this than you realize.

        Set a $_SESSION variable. Check/set it at the beginning of every page.

        session_start() ;
        if (isset($_SESSIO N['browser']) == false) $_SESSION['browser'] =
        yourDetectionFu nction();

        This will allow the rest of your adaptive code to still work, without
        constantly having to explicitly pass around something that should be
        the same on every single page during a session.

        ~D

        Comment

        • Michael Winter

          #5
          Re: Best way to deal with Javascript (in)compatibili ty using PHP?

          On 29/06/2005 13:39, Philip Ronan wrote:

          [snip]
          [color=blue]
          > http://www.google.co.uk/search?q=jav...ject+detection[/color]

          The term, object detection, probably isn't the best one to use. It has
          connotations with 'Browser detection via object inference', as you might
          notice by looking at some of the search results. A better phrase to
          consider is feature detection, and it is this principle that the OP
          should be trying to embrace.

          The actual browser that is in use should normally be of absolutely /no/
          concern. The only interest should be what the capabilities are of that
          browser (hence /feature/ detection).

          Feature detection typically involves a one-to-one analysis of the
          environment. Simply assuming that because the host supports - for
          instance, document.getEle mentById - that it must automatically support
          the entire W3C DOM Core module is clearly flawed (though that's what
          many seem to do). Instead, one determines what is necessary to perform a
          particular task, and then sets about finding if those methods and
          objects are indeed present in the host environment. Doing this in an
          efficient manner typically requires a thorough understanding of ECMAScript.

          The FAQ notes for comp.lang.javas cript gives a more in-depth treatment
          of why the various detection methods are flawed, and why feature
          detection is vastly superior (including some simpler examples). If you
          have particular issues, post there.

          See <URL:http://www.jibbering.c om/faq/faq_notes/not_browser_det ect.html>

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • Kenneth Downs

            #6
            Re: Best way to deal with Javascript (in)compatibili ty using PHP?

            christian9997@h otmail.com wrote:
            [color=blue]
            > Hi
            >
            > We have started off using a $_GET parameter to keep track of the user's
            > browser:
            >
            > We detect what browser the visitor is using when he first arrives on
            > our website then we do a redirect to the same page adding on
            > "&browser=I E" or "&browser=D OM" at the end of the url (and we keep the
            > browser parameter in the URL the whole time he is on the website), this
            > enables us to use the following PHP method:
            >
            > function javascript($nam e) {
            > if($_GET[brow] == "IE") {
            > return "document.all(' $name')";
            > }
            > else {
            > return "document.getEl ementById('$nam e')";
            > }
            > }
            >[/color]

            If find that putting this function into your javascript library solves the
            problem:

            function ob(oname) {
            if (document.getEl ementById)
            return document.getEle mentById(oname) ;
            else if (document.all)
            return document.all[name];
            }


            Once this is in there, you can write universal code that just refers to
            objects using the "ob('id')" function. Note that it is going by ID, not by
            name.

            --
            Kenneth Downs
            Secure Data Software, Inc.
            (Ken)nneth@(Sec )ure(Dat)a(.com )

            Comment

            • Manuel Lemos

              #7
              Re: Best way to deal with Javascript (in)compatibili ty using PHP?

              Hello,

              on 06/29/2005 09:24 AM christian9997@h otmail.com said the following:[color=blue]
              > We would be interested in hearing about the way people deal with
              > javascript browser compatibility using PHP. There must be some much
              > simpler ways of doing this but we are not sure how to do it.[/color]

              Often is not a matter of dealing with browser differences but also
              different Javascript versions in different versions of the same browser.

              The most reliable way to solve that problem is to provide alternate
              Javascript code that is conditionally executed at Javascript run time
              depending on what the browser supports.

              That is the solution used by this form validation class for instance to
              validate e-mail addresses. If the regular expression objects are
              available, it uses them, otherwise it uses a more basic type of e-mail
              address validation:




              if(s.search)
              {
              return (s.search(new RegExp('email-regular-expression','gi '))>=0)
              }
              else
              {
              some other type of basic validation;
              }

              Regular expression detection support is made checking the existence of
              the search method in the string object.

              --

              Regards,
              Manuel Lemos

              PHP Classes - Free ready to use OOP components written in PHP


              PHP Reviews - Reviews of PHP books and other products


              Metastorage - Data object relational mapping layer generator

              Comment

              Working...