Forum Moderators: open

Message Too Old, No Replies

PHP returning JavaScript

PHP returning JavaScript

         

coolbananas

11:20 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



Hi there,

I need to execute a dynamically created external JavaScript file(via PHP) within the "BODY" section of a HTML document. When I try to execute the script with these lines of code, no JavaScript is fetched:
<script type="text/javascript" src="..../myfile.php"></script>

My PHP looks something like this...

header("Cache-Control: no-store, no-cache");
header("Content-type: text/javascript");

The JavaScript is sent back with the "echo" function.

httpwebwitch

2:38 am on Apr 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi coolbananas, welcome to WebmasterWorld!
nice name.

you're doing it right - there is no reason your PHP-generated javascript shouldn't work, as you've described it above.

what do you see when you request the myfile.php script directly with a browser? are your echoes echoing?

coolbananas

11:01 am on Apr 16, 2008 (gmt 0)

10+ Year Member



That's what I don't understand. When I call the PHP file directly, the data is echoed. As soon as I put it in the BODY section using <script type="text/javascript" src="..../myfile.php"></script> nothing happens.

coolbananas

11:01 am on Apr 16, 2008 (gmt 0)

10+ Year Member



Btw, thanks for the warm welcome.

Receptional Andy

11:06 am on Apr 16, 2008 (gmt 0)



Should the content type be as below?

[quote]


Content-Type: application/x-javascript
[/quote]

coolbananas

11:20 am on Apr 16, 2008 (gmt 0)

10+ Year Member



I've changed that header now and still nothing. This is what is displayed in my browser when I run the PHP file:

document.write('<font size="3" face="Times" color=red>Shipping...');

Will this work?

httpwebwitch

1:27 am on Apr 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it looks OK, except that your <font> tag is deprecated and unclosed

how weird. I've used that technique countless times, and never had any problems with it.

Try rebooting your computer.

Or maybe it's your sound card!