PHP
  Home arrow PHP arrow Page 2 - 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 - PHP Disguised as JavaScript?
    ( Page 2 of 4 )

    One very useful feature of PHP is that it allows you to set header information for the document you are generating. This header information can control a wide variety of things – forcing the user’s browser to open the file in a certain application, or to redirect the user to another page, or to authenticate the user before proceeding, and so on.

    By default, PHP files are treated as an HTML file by a user’s browser, since the output rendered by the server is generally text or HTML. The default content type then for an HTML (or PHP) file is "text/html." When a browser encounters this content type, it opens the file itself and renders any HTML markup found within the file appropriately. This is good, as the primary purpose of an HTML page is to format and display information.

    A JavaScript file ("text/javascript"), however, is different. It will be rendered in plain text, as its primary purpose is not to display information, but to manage it through the use of statements and functions. Put another way, placing HTML markup in a JavaScript file will result in Javascript errors, not an HTML page. Likewise putting JavaScript in an HTML file (without the appropriate encapsulating tags) will also cause undesirable results in most cases.

    So how do we avoid this problem? The answer is simple: by telling our header functions to set the appropriate content type, we are able to tell the browser to treat a PHP file not as a "text/html" file but as a "text/javascript" file. To do this we simply place the following code at the top of our file:

    header( 'Content-Type: text/javascript' );

    Note: It is important to place the header() function call at the top of our PHP file. Placing it lower may result in compilation errors, as all header information must be sent before any other content is output.

    The implications of what we have just discussed may already be clear to you, but in case they are not, let us consider the benefits of generating a JavaScript file with PHP. JavaScript files control various aspects of the user’s browser and its display, ranging from simple elements of style and formatting to more complex validation and HTML generation. However, JavaScript is still generally written in a static nature –- that is, the code itself doesn’t change, only the data it is manipulating.

    In many cases where PHP is used to dynamically generate a page and its various HTML objects, this may be limiting. Our JavaScript code may need to know about every object in order to intelligently manage a single object. We can accomplish this goal by using the header tag mentioned above. PHP can now generate not only our HTML, but all associated Javascript code as well -- resulting in yet another level of complexity in our dynamic system.



     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek