PHP
  Home arrow PHP arrow Page 3 - PHP and JavaScript, Pooling Your Resources
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

PHP and JavaScript, Pooling Your Resources
By: Brian Vaughn
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 35
    2005-09-20


    Table of Contents:
  • PHP and JavaScript, Pooling Your Resources
  • PHP Disguised as JavaScript?
  • You Want More JavaScript?
  • How Does This Work?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PHP and JavaScript, Pooling Your Resources - You Want More JavaScript?
    ( Page 3 of 4 )

    So far we’ve talked about how to generate JavaScript using PHP, and how that Javascript can then interact with users on a real-time basis. However, our initial problem still remains: once our page has been rendered, our PHP code ceases to be –- leaving the management of both data and user interaction to JavaScript. Although Javascript offers many powerful features, relying on it solely from this point on may still be unnecessarily restricting.

    “What if we could allow JavaScript to call back to PHP whenever it wanted help?” you may be wondering. The answer is that we can. Using three simple lines of JavaScript code, we can dynamically add as many Javascript/PHP files as we want to a page, and in real time. This opens up the doors for an enormous number of user-friendly features that would not otherwise be possible. We’ll take a look into a few of those features shortly, but for now let’s look at how this can be accomplished. Consider the function below:

    function attach_file( p_script_url ) {
          // create new script element, set its relative URL, and load it
          script = document.createElement( 'script' );
          script.src = p_script_url;
          document.getElementsByTagName( 'head' )[0].appendChild( script );
    }

    The above function takes as a parameter a relative URL to an external file. (This file may be a plain JavaScript file, but it may also be a PHP file disguised as a JavaScript file, as previous discussed.) The URL is used to create a new script object, which is then attached to the current document’s <head> tag. In other words, this function will retrieve and attach an external JavaScript or PHP file to a page after the page has already been loaded.

    Let’s look at an example.



     
     
    >>> More PHP Articles          >>> More By Brian Vaughn
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek