Hi everyone…
I came across a problem few days back, it was about calling or embedding javascript result/response in AJAX response. I was working on one of the feature of PHP based site http://www.PingMyCompany.com
I found that, javascript can’t be called in AJAX response, possible reason being, AJAX response is build on server side while javascript is simply client side (I may be wrong here, OR there can be some other reason for this as well).
So what to do, what if we have to embed the output of some external JS file in AJAX response as below???
/****AJAX response START****/
/**********Some Response*******************/
/********** Some Response ******************/
/**********Response from some JS file*********/
<script src=”http://www.crunchbase.com/cbw/company/zigron.js” type=”text/javascript”>
</script>
/**********Some Response*******************/
/********** Some Response ******************/
/****AJAX response END****/
Now the response from JS file will not get embedded with AJAX response. So what I did to solve this issue is that I got the output of JS file in a PHP variable using file_get_content function as follows.
$contents = file_get_contents (”http://www.crunchbase.com/cbw/company/zigron.js”);
Alternatively you can do the same using cURL but file_get_content is really smart solution (if the JS is file is not suspicious…).
Once done, you must know the format of contents being returned by the JS file. Now you can do simple string manipulation on the contents to mangle it according to the requirements and just echo it where you want to embed it in AJAX response. I have removed document.write from the start of the string and escaped few of whitespace characters and that’s it….
****AJAX response START****
/**********Some Response*******************/
/********** Some Response ******************/
/**********Response from some JS file*********/
$content = file_get_contents (”http://www.crunchbase.com/cbw/company/zigron.js”);
$content = substr($content,16);
$content = substr($content,0, strlen ($content)-2);
$content = str_replace(”\\n”,”",$content);
$content = str_replace(”\\t”,”",$content);
$content = str_replace(’\\”‘,”",$content);
echo $content;
/**********Some Response*******************/
/********** Some Response ******************/
/****AJAX response END****/
You can see the working example by clicking here. Click on “CrunchBase Profile” tab button which show ajax response built using remote JS file.
Feel free to discuss..
Thanks.
Keep Smiling
September 14, 2008 at 10:34 am
I can still remember the lunch we had that day
I wish we could have more problems like this one
March 1, 2009 at 3:54 am
I stuff age 18 car insurance quotes .car insurance companies in tx was mexico auto insurance car r chubb auto insurance ,can 21st century car insurance
a search often cheapest car insurance in uk If was denver auto insurance .should brownsville cheap car insurance quotes online insurance for car fort collins .