Call php file as if it’s an external javascript
Posted by: WebDevJunkie in JavaScript, PHP, tags: external javascript, header, JavaScript, PHPI learned a handy trick today. I wanted to provide an external javascript so other websites could display some info from my site, but then realized it would be way better if I could somehow have them call a php file instead. Well you can.
So the page call will look just like the typical javascript call, except we are calling a php file:
<script language=”JavaScript” type=”text/javascript” src=”http://www.yoursite.info/yourfile.php”></script>
Now since you are calling the php file this way you need to make the php file appear to be a javascript document so to do that you need to add a Header property like this:
Header(“content-type: application/x-javascript”);
Then you need to make sure your output is in javascript format, since that is what you are outputting. So lets look at an example php file might look like – you can use this to try it out:
<?
Header(“content-type: application/x-javascript”);
$testvar=”what’s up doc?”;
echo “document.write(\”Bugs Bunny says: <b>” . $testvar . “</b>\”)”;
?>
Then just call that file. Notice I am using php’s echo to write out the javascript code document.write – if were to just echo it it would not show up on the page calling the javascript.
Ok, now lets get even fancier. We can pass some parameters to our file in the page call:
<script language=”JavaScript” src=”http://www.yoursite.com/yourfile.php?var=elmerfudd&ID=4″></script>
Then you can pick up those parameters in your php file using $HTTP_GET_VARS[] and do whatever you want with them.
April 2nd, 2008 at 12:55 am
You have made my day – I have been having the same problem and have been tearing my hair out trying to find a solution. Thank you!
August 29th, 2008 at 7:53 am
Many thanks for your posting! I’ve had this problem for a short while now and it’s been a real problem. I couldn’t find anything on the Microsoft website and the Adobe technical team had never heard of it! I made the changes you suggested and everything’s back to normal. Fantastic!
September 8th, 2008 at 5:17 pm
THANK YOU!!!!!!!!!!!!!!!!!
I was soooooooo frustrated.
You’re a God Send.
October 24th, 2008 at 3:35 am
Yes, that does fix it but how then do you go about getting the thumbnails BACK. I don’t want the constant crashing problem, but I know I’m rather fond of being able to browse my image folders with the help of the thumbnails.