View Full Version : including PHP generated array into .js file

03-20-2004, 02:26 PM
I am having php generate an array which needs to be accessed by a javascript function in a .js file.

I have tried including the following in the .js file:

document.write("<SCR" + "IPT LANGUAGE='JavaScript1.2' SRC='New_Arrays4_2.js.php' TYPE='text/javascript'><\/SCR" + "IPT>");

but this doesn't seem to work.

I know the output of the php file is ok because when I paste its output into the .js file it works fine.

Thank you for any help.

Willy Duitt
03-20-2004, 03:55 PM
Should work.
Looks good and is not throwing any errors.
Make sure the path to the external javascript file is correct.

What's with the .php?


03-20-2004, 03:59 PM
The questions is, does the server send it as the correct MIME type? PHP defaults to 'text/html', but this should be sent as 'applicaton/x-javascript'. (No, not 'text/javascript'.)

03-20-2004, 04:05 PM
the php is there because it is the php file set up to generate and print the needed data.

the js script is basically a hierarchical menu generator in which you are supposed to manually insert the menu elements.

I generated mine using the php script--it prints out this:

this.item0="Automation Systems"
this.icon_rel0= 0
this.item0_0="Automated Carts"
this.icon_rel0_0= 0

but don't know how to get the js file to access the data--the document .write that I used doesn't seem to work...

I will try changing the application type (I know path is ok)


03-20-2004, 04:08 PM
Observe that this is the MIME type I'm talking about, not the contents of the type attribute of the script element. Try this:
header("Content-type: application/x-javascript;charset=utf-8");

03-20-2004, 04:11 PM
sorry, but where is this placed?
Before I echo my data?

03-20-2004, 04:18 PM
Anywhere in the php file.

[edit] Listen to Nightfire, not me...

03-20-2004, 04:25 PM
Should go above anywhere that's printed to the browser, otherwise you get a 'cannot modify header. headers already sent..' error. Next time please only do one thread on the same topic, it's wasting peoples time answering in 2 threads for the same problem

03-20-2004, 04:33 PM
I wan't sure where was best to post (JS or php!)
thank you for your help.

03-20-2004, 04:54 PM
I realize the problem now--I had <br> lines in my code.

Mozilla helped in the debugging.

Many thanks to all of you!