Re: javascript in <head> or in <body> ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2007-08-12 at 21:27 -0400, tedd wrote:
> At 1:26 PM -0400 8/11/07, Robert Cummings wrote:
> >On Sat, 2007-08-11 at 12:15 -0400, tedd wrote:
> >  > Always (fishing for another apology opportunity) place javascript in
> >>  external files and call them in via the header. Keep the code
> >>  unobtrusive. There are ways to use javascript without having to mix
> >>  it into your html -- look up DOM scripting.
> >
> >I absolutely agree with unobtrusive JavaScript, but I do disagree with
> >you slightly on using external files. Generally speaking i keep large
> >bits of code (Especially libs) in external files, but a few lines of
> >script for a form that appears on one page I'll often be put in the head
> >section. This way the JavaScript is in the same file as the template for
> >which it is associated. My template engine will relocate the JavaScript
> >to the <head> section at compile time.
> 
> That's clever. I assume that for production work (i.e., for a 
> client), you can isolate the files you need and be totally 
> unobtrusive if you want.
> 
> I just described my method off-list to another person and it went like this:
> 
> I have a system for my site development work such that I simply 
> include one header and one footer and no matter where the project 
> directory is that I'm currently working on, the process will find the 
> one main common header and common footer and will add them to my demo 
> automagically.
> 
> My code to start, looks like this:
> 
> <?php include('../header.php') ?>
>     <h1> Hi </h1>
> <?php include('../footer.php') ?>
> 
>  From there I have a complete page -- from doctype to copyright, it's 
> all there and it validates.
> 
> If I need a javascript file locally, then I add it to the local 
> working directory by naming the file "a.js". Likewise, if I need an 
> additional css file, then I name it "a.css" and it's also included in 
> the call.
> 
> However, if I don't need those files, then none are included in my 
> local working directory and attempts to load those files will fail -- 
> however -- it doesn't matter if an attempt to load fails or not as 
> long as the files are not needed.
> 
> I think that's kind of clever, but I like Rocky and Bullwinkle as well.

I don't use a script to find my JavaScript files and CSS but I do
something similar to your includes for header and footer. The difference
being that my template engine will pull them in at compile time and not
at run-time. A lot of static content is pulled in this way and relocated
using compile-time accumulators. Then I use run-time accumulators to
flush content at run-time into the appropriate areas if necessary. This
allows me to create a layout that is compiled once at compile time but
with flush hooks for run-time. For instance for simplicity I use the
following tag in the <head> area to set up JavaScript:

<jinn:javaScriptSystem/>

This is the same as doing the following manually:

----------------
<jinn:accumulatorFlush name="javaScriptTags"/>
<jinn:accumulatorFlush name="javaScriptTags" dynamic="true"/>

<jinn:javaScript>
<jinn:accumulatorFlush name="javaScript"/>
<jinn:accumulatorFlush name="javaScript" dynamic="true"/>

    function javaScriptOnLoad()
    {
        <jinn:accumulatorFlush name="javaScriptOnLoad"/>
        <jinn:accumulatorFlush name="javaScriptOnLoad" dynamic="true"/>
    }

</jinn:javaScript>
-----------------

The body tag will have an onload="javaScriptOnLoad()". Then in a content
file I can add the following:

<jinn:accumulate name="javaScriptOnLoad">

    // some javascript code to modify the DOM

</jinn:accumulate>

And it will get relocated at compile time to the location of the static
flush statement: <jinn:accumulatorFlush name="javaScriptOnLoad"/>

Similarly within my code at run-time I can do the following:

$mAcc = &$this->getServiceRef( 'accManager' );
$acc = &$mAcc->getRef( 'javaScriptOnLoad' );

$acc->append
(
    ' // some javascript to modify the DOM '
)

And it will be get flushed into the content at the location of the
dynamic flush statement:

    <jinn:accumulatorFlush name="javaScriptOnLoad" dynamic="true"/>

So all in all, it's very simple for me to put JavaScript (and any
content for that matter) anywhere I please. I often use it for layout
areas (such as left pane, right pane, etc), menus, visitor notices, etc.

BTW if you're wondering why I have a <jinn:javaScript> tag it's because
who wants to remember the following:

---------------------------
<script type="text/javascript"><!--//--><![CDATA[//><!--

//--><!]]></script>
---------------------------

:)

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux