by Saurabh
10. January 2008 20:51
The beauty of PHPLiveX is its astute simplicity that makes it among the best AJAX libraries for PHP. The dev team has now shifted to focus to PHP5, so ver. 2.3 is the last version for PHP4. Although a bit late, it was realized that for the sae of brevity and maintaining clean & tidy code support for outputting the javascript to a file rather than inline was made. Only a minor version too late for developers working on PHP4 code. So although folks using PHPLiveX 2.4 and above get to enjoy a more readable HTML output, the ExternalJS property is not supported by any official build for PHP4. But adding it is surprisingly easy!
I have been working to AJAXify an old PHP4 app on client insistence, this app is deployed on PHP4 and hence cannot use ExternalJS property. And I like my HTML to tidy up. So this is what I did to add ExternalJS to PHPLiveX 2.3 that was used in the project.
Start by adding a new property ExternalJS to the class. And then modify the Run method to something like this:
1: function Run(){
2: $html = $this->CreateJS();
3: for($i=0;$i<count($this->FList);$i++){
4: $html .= $this->CreateFunction($this->FList[$i]);
5: }
6: if(empty($this->ExternalJS)){
7: echo "<script type='text/javascript' language='javascript'>" . $html . "</script>";
8: }
9: else{
10: if(!file_exists($this->ExternalJS)){
11: $fp = fopen($this->ExternalJS, "w");
12: if($fp != false){
13: fwrite($fp, $html);
14: fclose($fp);
15: }
16: else
17: echo "<script type='text/javascript' language='javascript'>" . $html . "</script>";
18:
19:
20: }
21: echo "<script type='text/javascript' src='{$this->ExternalJS}'></script>";
22: }
23: }
That's it! You can get rid of inline embedding of the PHPLiveX javascript. Similar to ver. 2.4 and above the call to Run() must not be inside the <script></script> tags. You can download a modded ver. 2.3 file with ExternalJS property from here.
You can further enhance this code a little more, like - Restrict the ExternalJS property to not use paths or you could restrict it to a bunch of desired paths.