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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    including PHP generated array into .js file

    Hello,
    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.
    Patty

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    .....Willy

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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'.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.url0='get.php?id=1'
    this.item0_0="Automated Carts"
    this.icon_rel0_0= 0
    this.url0_0='get.php?id=2'

    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)

    thanks,
    Patty

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Observe that this is the MIME type I'm talking about, not the contents of the type attribute of the script element. Try this:
    PHP Code:
    <?php 
    header
    ("Content-type: application/x-javascript;charset=utf-8"); 
    ?>
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry, but where is this placed?
    Before I echo my data?
    patty

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Anywhere in the php file.

    [edit] Listen to Nightfire, not me...
    Last edited by liorean; 03-20-2004 at 03:27 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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

  • #9
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry!
    I wan't sure where was best to post (JS or php!)
    thank you for your help.
    patty

  • #10
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I realize the problem now--I had <br> lines in my code.

    Mozilla helped in the debugging.

    Many thanks to all of you!


  •  

    Posting Permissions

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