This is what we call external javascript too..

Anyway save your .js file as .php. For example, your js file will be named as file.php

The syntax to referencing a PHP file using external JavaScript is almost same as what we normally do for JS files:

<script type=”text/javascript” src=”file.php”></script>

where “file.php” is either an absolute or relative path to a PHP script instead of the usual .js file. Paramenters can also be passed to the PHP file by using the URL string:

<script type=”text/javascript” src=”file.php?id=3&name=hmmmsup”></script>

PHP Script can get these parameters using the global variable $_GET. I know, you may be wondering whats the catch here? Hmmm, actually there is no catch involved here, just some limitations. Since the PHP script is being invoked indirectly and via JavaScript, the final output of the PHP script should be a valid JavaScript. It can be considered as a dynamic .js file!

In the normal PHP script called inside a PHP page, the output is a raw HTML data and the source code can be modified using the script, but thats not the case with JavaScript invoked version though…Anyway there are loads of things which can be done using this!

ps: it had been a long day!

5 Comments

  1. YES :D there is a lot of fun to be had with this.
    Other fun tricks can include:
    - using addhandler or addtype or whichever to have php actually parse .js files in some directories
    - use mod_rewrite to just make the .php look like .js

    Those will just change the file extension. That’s not very useful, really ;) but for some they might be worth thinking about.

    But no, the most fun and important part of using php to generate your javascript (apart from it being dynamic) – for me, at least – is being able to control the headers! You can send instructions about mime types, caching, the weather, feeding goldfish…. anything you want! You can do that with apache as well, of course, but if you’re going to be using php to make dynamic scripts then you may as well play with the headers using php, too, no?

    Firefox has an extension called Firebug for debugging Javascript.
    There is an extension availalble which in turn extends firefub. I think it’s called phpbug. It sends debugging info back to phpbug through the headers (JSON encoded) — so you could have a perfectly valid script, and debug the php part of it without breaking it, through a console.
    I hope this helps someone.

  2. Don’t forget adding the proper header in the PHP file before returning any content. This shuould be your first line:

    If you don’t, Apache (or your web server) will serve it as plain html and some browsers could not work as expected.

    Regards,
    Jose.

  3. Hi,

    Got good thing here.I used .php file inclusion for images but never used for javascript..!! Anyways i like the idea and also liked idea of “SneakyWho_am_i” for using mod_rewrite.ya we can use this apache module for making fool for visitors.

    -inder singh

  4. Oh o ho! very nice site!u

  5. good work


Post a Comment

*
*