Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    JS equivalent for PHP's <?=$foo?> ?

    Is there something similar to PHP's
    PHP Code:
    <?=$foo?>
    statement?

    I want to define aload of variables as the page loads and then have them loaded into the right elements, without the need of something like this:

    PHP Code:
    document.getElementById("div1").innerHTML cont1;
    document.getElementById("div2").innerHTML cont2;
    document.getElementById("div3").innerHTML cont3;
    document.getElementById("div4").innerHTML cont4;
    document.getElementById("div5").innerHTML cont5;
    document.getElementById("div6").innerHTML cont6
    Last edited by martynball; 05-15-2013 at 10:39 PM.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    <?=$foo?> equals? <script>document.write(foo)</script>

  • Users who have thanked DaveyErwin for this post:

    martynball (05-15-2013)

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by martynball View Post
    I want to define aload of variables as the page loads and then have them loaded into the right elements, without the need of something like this:
    I can understand the desire, but PHP and JS produce output in totally different environments. PHP just writes to the output stream (which goes most of the times into the HTTP Response) while JS usually does not have a direct output stream* but has to modify a model (the DOM) in an application (the browser) that renders the visible result.

    if you compare PHP with the Node JS application (also server software), the differences are much less. Node can write output via console (to the server terminal) and Request.send() (to the HTTP Response), much like PHP is using echo/print.


    * - you could see the console interface as direct output stream, though.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,461
    Thanks
    0
    Thanked 632 Times in 622 Posts
    If you have both PHP and JavaScript then any document.write statements in the JavaScript should be rewritten to use PHP echo calls instead.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by martynball View Post
    I want to define aload of variables as the page loads and then have them loaded into the right elements, without the need of something like this:
    PHP Code:
    document.getElementById("div1").innerHTML cont1;
    document.getElementById("div2").innerHTML cont2;
    document.getElementById("div3").innerHTML cont3;
    document.getElementById("div4").innerHTML cont4;
    document.getElementById("div5").innerHTML cont5;
    document.getElementById("div6").innerHTML cont6
    What in the world is wrong with "something like this"????

    If you simply don't like the repetitive nature of it, then do something like this:
    Code:
    var content = {
        div1 : "content for div1",
        div2 : "and content for div2",
        div3 : "stuff for div3",
        ...
        div47 : "last stuff"
    };
    for ( var id in content )
    {
        document.getElementById(id).innerHTML = content[id];
    }
    Yes, you can use document.write(), as Davey noted. But it's a crappy way to do it. You *must* do that *INLINE* in the page, as it is being created. You can't use it once the page is loaded. Don't do it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •