accessing DOM

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J. J. Cale

    accessing DOM

    I want to access the DOM. Can something like this be done with PHP or a
    combination of PHP/JAVASCRIPT? not tested!
    <html><head><ti tle>DOM test</title>
    <?php
    $inFileName = 'test.js';
    $outFileName = 'jk.js';
    function doIt() {
    $dd = getElementById( 'first');
    $dd.innerText=$ outFileName;
    }
    ?>
    </head>
    <body>
    <p id="first" onclick="doIt() ">ClickMe<b r>
    <p onclick="first. innerText=$inFi leName">ClickMe <br>
    </body></html>


  • Simon Stienen

    #2
    Re: accessing DOM

    "J. J. Cale" <photom@netvisi on.net.il> wrote:[color=blue]
    > I want to access the DOM. Can something like this be done with PHP or a
    > combination of PHP/JAVASCRIPT? not tested!
    > <html><head><ti tle>DOM test</title>
    > <?php
    > $inFileName = 'test.js';
    > $outFileName = 'jk.js';
    > function doIt() {
    > $dd = getElementById( 'first');
    > $dd.innerText=$ outFileName;
    > }
    > ?>
    > </head>
    > <body>
    > <p id="first" onclick="doIt() ">ClickMe<b r>
    > <p onclick="first. innerText=$inFi leName">ClickMe <br>
    > </body></html>[/color]

    Of course you can do it, you just have to assure the PHP code will not be
    parsed by your server and you have to find a browser which allows PHP, in
    extension to javascript, as client side scripting language.

    OTOH, if you want the client side javascript to call a server side PHP
    function, you have to generate a completely new HTTP-Request using
    document.locati on= or similar, resulting in a *completely* new page or in
    *no* new page (for example HTTP 204: No Content).
    If you want to *modify* the current page from within PHP using PHP on
    javascript variables that cleary shows you didn't understand the concept of
    a server side script language.
    --
    Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
    »What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done.«
    -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

    Comment

    • david

      #3
      Re: accessing DOM

      [original snipped]
      [color=blue]
      > If you want to *modify* the current page from within PHP using PHP on
      > javascript variables that cleary shows you didn't understand the concept
      > of
      > a server side script language.[/color]

      With all due respect, this is of course, entirely possible. Perhaps before
      you say that people do not understand, you should first make sure that you
      understand it perfectly.



      On first blush, you would seem correct: JavaScript is client side, PHP is
      server side, and never the two shall mix. Yes, of course, you can use PHP to
      write JavaScript as the page is rendered, and yes of course, this works
      well.



      But, think about this... place an inline frame (hidden) on the page to
      modify the JavaScript variables from. Someone on your page is something that
      the user does (presumably; perhaps they select an item from a drop down
      list) that should cause a change. To make this happen, add a JavaScript
      function that replaces the URL of the inline frame. The new URL loads a PHP
      page which presumably does something (perhaps it gets data from a database).
      As the page finishes, it writes a JavaScript function that calls back to the
      opening page (the opener. syntax) and executes a function there; this
      function can now happily change variables in JavaScript (because it IS
      JavaScript). PHP can change JavaScript.



      I will grant you it is not a straightforward approach. I will also tell you
      that Apple has documented it quite well, and I have used it in production
      for over two years without a hitch. In my experience (a nice way to say
      things), this is a stable, reliable solution.



      OK. Just my two cents.

      david


      Comment

      • Simon Stienen

        #4
        Re: accessing DOM

        david <someone@micros oft.com> wrote:[color=blue]
        > [original snipped]
        >[color=green]
        >> If you want to *modify* the current page from within PHP using PHP on
        >> javascript variables that cleary shows you didn't understand the concept
        >> of
        >> a server side script language.[/color]
        >
        > With all due respect, this is of course, entirely possible. Perhaps before
        > you say that people do not understand, you should first make sure that you
        > understand it perfectly.[/color]

        Well... Maybe you have information I didn't know...
        [color=blue]
        > On first blush, you would seem correct: JavaScript is client side, PHP is
        > server side, and never the two shall mix. Yes, of course, you can use PHP to
        > write JavaScript as the page is rendered, and yes of course, this works
        > well.[/color]

        That's how far I was in the statement.
        [color=blue]
        > But, think about this... place an inline frame (hidden) on the page to
        > modify the JavaScript variables from. Someone on your page is something that
        > the user does (presumably; perhaps they select an item from a drop down
        > list) that should cause a change. To make this happen, add a JavaScript
        > function that replaces the URL of the inline frame. The new URL loads a PHP
        > page which presumably does something (perhaps it gets data from a database).
        > As the page finishes, it writes a JavaScript function that calls back to the
        > opening page (the opener. syntax) and executes a function there; this
        > function can now happily change variables in JavaScript (because it IS
        > JavaScript). PHP can change JavaScript.[/color]

        Ok, so far... But: It's not the PHP changing the JavaScript but the
        JavaScript which was written by the PHP in the inline frame, sounds like
        hair-splitting, but it *is* a difference. Further, you are not modifying
        the *current* page (which is the one in the iframe and completely
        reloaded), but the parent page which contains the iframe.

        I know this seems obvious, but if you take the small technical details,
        you'll see, that my previous posting perfectly matches your case, too.
        --
        Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
        »What you do in this world is a matter of no consequence,
        The question is, what can you make people believe that you have done.«
        -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

        Comment

        • david

          #5
          Re: accessing DOM

          Simon:
          [color=blue]
          > Well... Maybe you have information I didn't know...[/color]

          I didn't know this either until I stumbled across one fine day. <grin>
          [color=blue]
          > Ok, so far... But: It's not the PHP changing the JavaScript but the
          > JavaScript which was written by the PHP in the inline frame, sounds like
          > hair-splitting, but it *is* a difference. Further, you are not modifying
          > the *current* page (which is the one in the iframe and completely
          > reloaded), but the parent page which contains the iframe.
          >
          > I know this seems obvious, but if you take the small technical details,
          > you'll see, that my previous posting perfectly matches your case, too.[/color]

          I have no wish to split hairs, and you are correct as well.

          To me, it seemed the OP wanted the ability to affect JavaScript from PHP
          (again, presumably because they are digging information out of a database or
          other backend system/process, which I do not know how to do with just
          JavaScript.); what I was suggesting does just that. The end result is that
          PHP, um, adjusts JavaScript. It is just in a rather odd roundabout way. But,
          I think the important thing is that the end user does not seem all the
          backend stuff, and at the end of the day, that is the most important thing:
          usability and a friendly page.

          OK. Thanks for a good reply, Simon! I hope you have a most excellent day! (I
          like it when everyone is right!)
          david



          Comment

          • Chung Leong

            #6
            Re: accessing DOM

            > OTOH, if you want the client side javascript to call a server side PHP[color=blue]
            > function, you have to generate a completely new HTTP-Request using
            > document.locati on= or similar, resulting in a *completely* new page or in
            > *no* new page (for example HTTP 204: No Content).
            > If you want to *modify* the current page from within PHP using PHP on
            > javascript variables that cleary shows you didn't understand the concept[/color]
            of[color=blue]
            > a server side script language.[/color]

            You can actually do that in ASP.NET. You can attach server-side functions to
            client-side events. You can also modify the document via the DOM on the
            server-side.


            Comment

            • Berislav Lopac

              #7
              Re: accessing DOM

              On Sat, 25 Sep 2004 09:16:16 +0200, J. J. Cale wrote:
              [color=blue]
              > I want to access the DOM. Can something like this be done with PHP or a
              > combination of PHP/JAVASCRIPT? not tested!
              > <html><head><ti tle>DOM test</title>
              > <?php
              > $inFileName = 'test.js';
              > $outFileName = 'jk.js';
              > function doIt() {
              > $dd = getElementById( 'first');
              > $dd.innerText=$ outFileName;
              > }
              > ?>
              > </head>
              > <body>
              > <p id="first" onclick="doIt() ">ClickMe<b r>
              > <p onclick="first. innerText=$inFi leName">ClickMe <br>
              > </body></html>[/color]

              DOM is a standards-defined XML API, not a Javascript feature. Many of
              languages have DOM support, including PHP.

              Be aware, however, that DOM support in PHP4 is not very good; I strongly
              suggest to use PHP5 to use DOM, if possible.

              Berislav

              Comment

              Working...