Is a PHP variable supposed to be seen in a .js file included into a .php file

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

    Is a PHP variable supposed to be seen in a .js file included into a .php file

    Is a PHP variable supposed to be seen in a .js file included into a .php
    file?

    I have a client side javascript code stored in a .js file which is included
    into a PHP file using
    <script src="filename.j s></script>

    This code initialises a Javascript variable
    var u="string";

    The string is supposed to be the value of $HTTP_HOST

    So, when initialise this variable like

    var u=<? echo $HTTP_HOST;?>;

    I sterted getting a javascript message Syntax Error

    Am I doing something wrong, or PHP variables are not seen in included js
    files?






  • Justin Koivisto

    #2
    Re: Is a PHP variable supposed to be seen in a .js file includedinto a .php file

    aa wrote:
    [color=blue]
    > Is a PHP variable supposed to be seen in a .js file included into a .php
    > file?
    >
    > I have a client side javascript code stored in a .js file which is included
    > into a PHP file using
    > <script src="filename.j s></script>
    >
    > This code initialises a Javascript variable
    > var u="string";
    >
    > The string is supposed to be the value of $HTTP_HOST
    >
    > So, when initialise this variable like
    >
    > var u=<? echo $HTTP_HOST;?>;
    >
    > I sterted getting a javascript message Syntax Error
    >
    > Am I doing something wrong, or PHP variables are not seen in included js
    > files?[/color]

    js files are not parsed for PHP code unless you specifically tell the
    webserver to do so. Therefore, "var u=<? echo $HTTP_HOST;?>;" in the .js
    will error since the browser doesn't know what to do.

    --
    Justin Koivisto - spam@koivi.com

    Comment

    • Kelvin Mackay

      #3
      Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

      You need to modify your web server configuration so that .js files are
      parsed by PHP.
      For Apache, use something like the following:

      AddType application/x-httpd-php .js

      Kelvin

      "aa" <aa@virgin.ne t> wrote in message
      news:416416cb$0 $54818$ed2619ec @ptn-nntp-reader01.plus.n et:[color=blue]
      > Is a PHP variable supposed to be seen in a .js file included into a .php
      > file?
      >
      > I have a client side javascript code stored in a .js file which is
      > included
      > into a PHP file using
      > <script src="filename.j s></script>
      >
      > This code initialises a Javascript variable
      > var u="string";
      >
      > The string is supposed to be the value of $HTTP_HOST
      >
      > So, when initialise this variable like
      >
      > var u=<? echo $HTTP_HOST;?>;
      >
      > I sterted getting a javascript message Syntax Error
      >
      > Am I doing something wrong, or PHP variables are not seen in included js
      > files?[/color]

      ---
      Mail is certified Virus Free.
      Checked by AVG anti-virus system (http://www.grisoft.com).
      Version: 6.0.773 / Virus Database: 520 - Release Date: 05/10/2004


      Comment

      • aa

        #4
        thanks



        Comment

        • Daniel Tryba

          #5
          Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

          Kelvin Mackay <no.kelvin.sp.m ackay.am@remove .nospam.parts.g mail.com> wrote:[color=blue]
          > You need to modify your web server configuration so that .js files are
          > parsed by PHP.
          > For Apache, use something like the following:
          >
          > AddType application/x-httpd-php .js[/color]

          Why not simply point the src to a php file, that way php isnt executed
          for every other static js file. Just don't forget to set the correct
          mimetype (application/x-javascript).

          BTW TOFU is bad practice.

          --

          Daniel Tryba

          Comment

          • Ehtor

            #6
            Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

            At the extreme risk of going way off topic...

            I believe that TOFU actually makes much more sense.

            When you convince google-groups to preview the BOTTOM 30 lines of a
            message, I'll switch over..

            I find it hard to understand how anyone who uses a modern, message
            threading news reader can tolerate bottom postings... I'm sure someone
            will let me know.. :-)

            R






            Daniel Tryba <news_comp.lang .php@canopus.nl > wrote in
            news:ck19pi$edn $1@news.tue.nl:
            [color=blue]
            > Kelvin Mackay <no.kelvin.sp.m ackay.am@remove .nospam.parts.g mail.com>
            > wrote:[color=green]
            >> You need to modify your web server configuration so that .js files
            >> are parsed by PHP.
            >> For Apache, use something like the following:
            >>
            >> AddType application/x-httpd-php .js[/color]
            >
            > Why not simply point the src to a php file, that way php isnt executed
            > for every other static js file. Just don't forget to set the correct
            > mimetype (application/x-javascript).
            >
            > BTW TOFU is bad practice.
            >[/color]

            Comment

            • Default User

              #7
              Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

              Ehtor wrote:
              [color=blue]
              > At the extreme risk of going way off topic...
              >
              > I believe that TOFU actually makes much more sense.
              >
              > When you convince google-groups to preview the BOTTOM 30 lines of a
              > message, I'll switch over..[/color]


              The fact that some people don't know how to snip when using the correct
              form of posting doesn't justify you using an incorrect form.




              Brian

              Comment

              • Kelvin Mackay

                #8
                Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

                You're right, that would work better :-)

                However, you could also put the directive in a .htaccess file in the
                directory with the dynamic js files.
                I personally prefer to give files their correct extensions so I don't
                lose syntax highlighting (etc) in editors that don't allow you to
                manually specify a file's type.

                Forgive my ignorance, what does TOFU stand for?

                --

                Kelvin

                "Daniel Tryba" <news_comp.lang .php@canopus.nl > wrote in message
                news:ck19pi$edn $1@news.tue.nl:[color=blue]
                > Kelvin Mackay <no.kelvin.sp.m ackay.am@remove .nospam.parts.g mail.com>
                > wrote:
                >[color=green]
                > > You need to modify your web server configuration so that .js files are
                > > parsed by PHP.
                > > For Apache, use something like the following:
                > >
                > > AddType application/x-httpd-php .js[/color]
                >
                >
                > Why not simply point the src to a php file, that way php isnt executed
                > for every other static js file. Just don't forget to set the correct
                > mimetype (application/x-javascript).
                >
                > BTW TOFU is bad practice.
                >[/color]

                ---
                Mail is certified Virus Free.
                Checked by AVG anti-virus system (http://www.grisoft.com).
                Version: 6.0.773 / Virus Database: 520 - Release Date: 05/10/2004


                Comment

                • Daniel Tryba

                  #9
                  Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

                  Ehtor <nomail@nomail. com> wrote:[color=blue]
                  > At the extreme risk of going way off topic...
                  >
                  > I believe that TOFU actually makes much more sense.
                  >
                  > When you convince google-groups to preview the BOTTOM 30 lines of a
                  > message, I'll switch over..[/color]

                  30 lines under a full overquote is just as bad (at least), if you write
                  a posting you should take the time to make a readable posting, it should
                  be short and summarize for readability (just in case some messages don't
                  make it or get delivered out of order). TOFU or bottom posting are for
                  lazy people who write for themselves.
                  [color=blue]
                  > I find it hard to understand how anyone who uses a modern, message
                  > threading news reader can tolerate bottom postings... I'm sure
                  > someone will let me know.. :-)[/color]

                  So whats the reason for the full underquote? Modern threaded readers
                  make it easy for the user to get the full message if they want it to!
                  TOFU makes no sense in this case, so just don't resend the same old
                  article so people don't have to scan in just in case there is something
                  added.

                  --

                  Daniel Tryba

                  Comment

                  • Michael Fesser

                    #10
                    Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

                    .oO(Ehtor)
                    [color=blue]
                    >I find it hard to understand how anyone who uses a modern, message
                    >threading news reader can tolerate bottom postings...[/color]

                    Saves time, because you don't have to endlessly scroll down or look up
                    in another posting what the author is referring to.
                    [color=blue]
                    >I'm sure someone
                    >will let me know.. :-)[/color]

                    The usual (and for many people most readable) way is:
                    [color=blue]
                    >question[/color]

                    answer
                    [color=blue]
                    >question[/color]

                    answer

                    Like in any other discussion. Or do you wait until you got 5 questions
                    and then answer them all at once? If yes - how would you do this? Always
                    say something like "And now to your question about this and that"? Why
                    not simply answer/comment one question at a time?

                    Another thing that people writing top postings (BTW: what's TOFU in
                    English? I only know it in German ... *g*) seem to forget is the length
                    of the posting. With just adding an answer on top of another posting the
                    articles get longer and longer, hundreds of lines for just a handful of
                    new text. This wastes bandwidth and space on hundreds of newsservers.

                    Micha

                    Comment

                    • Tim Roberts

                      #11
                      Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

                      "aa" <aa@virgin.ne t> wrote:[color=blue]
                      >
                      >Is a PHP variable supposed to be seen in a .js file included into a .php
                      >file?
                      >
                      >I have a client side javascript code stored in a .js file which is included
                      >into a PHP file using
                      ><script src="filename.j s></script>[/color]

                      You need to think about which side (client/server) is doing what. PHP is
                      running on the server. As far as PHP knows, that <script> tag is just
                      another HTML tag. It's just a string of text to be sent to the client.

                      By the time the client browser sees this tag, PHP is all done. The browser
                      makes another, separate request to the server to fetch the URL
                      "filename.j s". PHP is not involved in that transaction at all -- it's just
                      a simple fetch.

                      If you really need to so substitution in your .js file, then you need to
                      have it included in the PHP source, using something like this:

                      <script>
                      <? require( "filename.j s" ); ?>
                      </script>

                      That way, PHP will interpret the contents of the file and do substitutions.
                      [color=blue]
                      >Am I doing something wrong, or PHP variables are not seen in included js
                      >files?[/color]

                      PHP variables can only be seen in files that are processed by PHP.
                      --
                      - Tim Roberts, timr@probo.com
                      Providenza & Boekelheide, Inc.

                      Comment

                      • aa

                        #12
                        Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

                        Thanks, it never occured to me that the <script> tag can src any file, not
                        just .js

                        Regarding setting the correct mimetype (application/x-javascript) - where
                        do I set it?




                        "Daniel Tryba" <news_comp.lang .php@canopus.nl > wrote in message
                        news:ck19pi$edn $1@news.tue.nl. ..[color=blue]
                        > Kelvin Mackay <no.kelvin.sp.m ackay.am@remove .nospam.parts.g mail.com>[/color]
                        wrote:[color=blue][color=green]
                        > > You need to modify your web server configuration so that .js files are
                        > > parsed by PHP.
                        > > For Apache, use something like the following:
                        > >
                        > > AddType application/x-httpd-php .js[/color]
                        >
                        > Why not simply point the src to a php file, that way php isnt executed
                        > for every other static js file. Just don't forget to set the correct
                        > mimetype (application/x-javascript).
                        >
                        > BTW TOFU is bad practice.
                        >
                        > --
                        >
                        > Daniel Tryba
                        >[/color]


                        Comment

                        • aa

                          #13
                          Thank you very much, Tim, for the perfect explanation if the situation





                          Comment

                          • aa

                            #14
                            Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

                            "BTW TOFU is bad practice."

                            I don't care if the reply it typed at the top or at the bottom as long as it
                            is a non-bullshit reply.

                            I personally do not like scrolling down prevuious posting to get the reply
                            (I now suspect that the expression "to get to the bottom of something"
                            meaning to understand somethiong through hard labour, originated from
                            reading such threads :-) ).

                            But I do not feel like imposing my preferences on others and only call upon
                            the other not to raise similar discussions as they indeed clog the group.

                            Thenky everybody who replied both at the top and at the bottom.


                            "Daniel Tryba" <news_comp.lang .php@canopus.nl > wrote in message
                            news:ck19pi$edn $1@news.tue.nl. ..[color=blue]
                            > Kelvin Mackay <no.kelvin.sp.m ackay.am@remove .nospam.parts.g mail.com>[/color]
                            wrote:[color=blue][color=green]
                            > > You need to modify your web server configuration so that .js files are
                            > > parsed by PHP.
                            > > For Apache, use something like the following:
                            > >
                            > > AddType application/x-httpd-php .js[/color]
                            >
                            > Why not simply point the src to a php file, that way php isnt executed
                            > for every other static js file. Just don't forget to set the correct
                            > mimetype (application/x-javascript).
                            >
                            > BTW TOFU is bad practice.
                            >
                            > --
                            >
                            > Daniel Tryba
                            >[/color]


                            Comment

                            • aa

                              #15
                              Re: Is a PHP variable supposed to be seen in a .js file included into a .php file

                              Just one clarification, Tim

                              I thought that PHP first performs all include() and require() and only then
                              execute whateever is found incide <? ?> including substitutions. That is why
                              a variable declared in one included file is seen from the other included
                              file.

                              However by that token the code
                              <?
                              $fn="filename";
                              include ($fn);
                              ?>
                              should not work because
                              include ($fn);
                              should be done BEFORE executing $fn="filename";

                              This is the case with ASP's #include directive
                              But it works in PHP which means that PHP execute PHP code on the hosting
                              page in the order it is written, adding included external files as it goes
                              and executing PHP on the included files as it goes through them.
                              Is that how it works?


                              Comment

                              Working...