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 6 of 6
  1. #1
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using PHP in external JS file

    I have a JS file linked with <script src="...">, which I want to use PHP variables in. Is there any way of doing this? I tried a simple test:
    PHP Code:
    <?php echo "alert('hello world!')" ?>
    Which gave me a syntax error (the php code was not executed).

    Is there something like eval(); that I can use? (Note: I want the file to remain external, it's not the shortest of files)
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #2
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    trying renaming your javascript file from script.js to script.php
    "There is more than one way to do it."

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    maybe
    PHP Code:
    echo "<script>alert('hello world');</script>"
    ?

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think dswimboy is right. If you don't want to rename your file, you'll have to add the .js extension to your server to be parsed as a PHP file.

    Sadiq.

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Posts
    232
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, I tested it all you got to do is:
    PHP Code:
    echo ("<script src = 'wahteveritis.js'></script>"
    Or the tested thing:
    PHP Code:
    echo ("<script>alert('Hello World')</script>"

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    stophon, I think you misunderstood me's question: He wants to use PHP in his external JavaScript file, not in the file where the script gets included. And to reiterate: Both dswimboy and sad69 are right, the server needs to hand over the javascript file to the PHP interpreter before sending it to the client. The easiest way is by renaming the file, another possiblity would be to use a .htaccess file with

    Code:
    <Files external.js>
    ForceType application/x-httpd-php
    </Files>
    in it. Presumes that you're running this on Apache, and have permissions to use .htaccess files. And of course external.js must be the name of the JS file in question.
    De gustibus non est disputandum.


  •  

    Posting Permissions

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