469,570 Members | 1,676 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,570 developers. It's quick & easy.

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

aa
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.js></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?


Jul 17 '05 #1
19 2091
aa wrote:
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.js></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?


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 - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #2
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.net> wrote in message
news:41***********************@ptn-nntp-reader01.plus.net:
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.js></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?


---
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
Jul 17 '05 #3
aa


Jul 17 '05 #4
Kelvin Mackay <no********************@remove.nospam.parts.gmail. com> wrote:
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


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

Jul 17 '05 #5
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 <ne****************@canopus.nl> wrote in
news:ck**********@news.tue.nl:
Kelvin Mackay <no********************@remove.nospam.parts.gmail. com>
wrote:
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


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.


Jul 17 '05 #6
Ehtor wrote:
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..

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
Jul 17 '05 #7
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" <ne****************@canopus.nl> wrote in message
news:ck**********@news.tue.nl:
Kelvin Mackay <no********************@remove.nospam.parts.gmail. com>
wrote:
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

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.


---
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
Jul 17 '05 #8
Ehtor <no****@nomail.com> wrote:
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..
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.
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.. :-)


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

Jul 17 '05 #9
.oO(Ehtor)
I find it hard to understand how anyone who uses a modern, message
threading news reader can tolerate bottom postings...
Saves time, because you don't have to endlessly scroll down or look up
in another posting what the author is referring to.
I'm sure someone
will let me know.. :-)
The usual (and for many people most readable) way is:
question
answer
question


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
Jul 17 '05 #10
"aa" <aa@virgin.net> wrote:

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.js></script>
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.js". 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.js" ); ?>
</script>

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


PHP variables can only be seen in files that are processed by PHP.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 17 '05 #11
aa
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" <ne****************@canopus.nl> wrote in message
news:ck**********@news.tue.nl...
Kelvin Mackay <no********************@remove.nospam.parts.gmail. com>

wrote:
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


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

Jul 17 '05 #12
aa


Jul 17 '05 #13
aa
"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" <ne****************@canopus.nl> wrote in message
news:ck**********@news.tue.nl...
Kelvin Mackay <no********************@remove.nospam.parts.gmail. com>

wrote:
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


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

Jul 17 '05 #14
aa
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?
Jul 17 '05 #15
"aa" <aa@virgin.net> wrote in message
news:41***********************@ptn-nntp-reader04.plus.net:
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?
Place the line:
header('Content-type: application/x-javascript');
near the beginning of your script - it must be before any output is sent
to the browser. For more information have a gander at
http://www.php.net/header/


"Daniel Tryba" <ne****************@canopus.nl> wrote in message
news:ck**********@news.tue.nl...
Kelvin Mackay <no********************@remove.nospam.parts.gmail. com>


wrote:
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

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


---
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
Jul 17 '05 #16
"Ehtor" <no****@nomail.com> wrote in message
news:Xn*****************************@140.99.99.130 :
At the extreme risk of going way off topic...

I believe that TOFU actually makes much more sense.
[snip]

I never meant to start this!
Now I know what TOFU is :)
I see everyone making valid points, I suppose in the end it comes down
to preference. However, I've never seen anyone complaining about people
NOT using TOFU, so I think I'll adopt this inline practice.
And see; I snipped too.

Sorry for any offence or inconvenience I caused.

R


Daniel Tryba <ne****************@canopus.nl> wrote in
news:ck**********@news.tue.nl:

[snip]

BTW TOFU is bad practice.


---
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
Jul 17 '05 #17
"aa" <aa@virgin.net> wrote:

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.
No. include() and require() are just executable functions like every other
function. They are not executed until they are encountered in the source
code, UNLIKE C and ASP where #include files are included before parsing
starts.

However by that token the code
<?
$fn="filename";
include ($fn);
?>
should not work because
include ($fn);
should be done BEFORE executing $fn="filename";
Nope. This works fine.
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?


Yes, exactly.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 17 '05 #18
Michael Fesser <ne*****@gmx.net> wrote in
news:h1********************************@4ax.com:

<snip>
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


Pretty close in either language:

English TOFU = Text Over, Fullquote Under
German TOFU = "Text oben, Fullquote unten"


Jul 17 '05 #19
Daniel Tryba <ne****************@canopus.nl> wrote in
news:ck**********@news.tue.nl:

<snip>

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.


You're 100% correct. If I full posted an underquote that was my bad.

Either way, the flames here are no where near as bad as if you top post in
one of the OS or C/C++ groups.... Nevertheless, I will need to make a
lifestyle change to avoid being plonked.
Jul 17 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by nielson | last post: by
4 posts views Thread by Deane Barker | last post: by
5 posts views Thread by flemming eriksen | last post: by
2 posts views Thread by Dennis M. Straussfogel | last post: by
reply views Thread by Rick Casey | last post: by
1 post views Thread by Arthur Dent | last post: by
4 posts views Thread by subramanian100in | last post: by
11 posts views Thread by whirlwindkevin | last post: by
8 posts views Thread by aki | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.