Re: call a javascript within php

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

 



At 09:59 AM 7/30/2006, Jochen Kaechelin wrote:
How can I call a JavaScript inside php?


You can't literally call client-side JavaScript from server-side PHP because they're executing at different times and in different computers. PHP executes on the server then downloads the page to the browser, then JavaScript executes in the browser.

What you can do is to write your PHP script so that it generates your JavaScript code as part of the page download, set to execute automatically when the page downloads. The JavaScript can either be inline in the HTML page itself or linked to the HTML page with a script tag and a SRC attribute that produces the desired JavaScript on the fly, e.g.:

        echo <<< hdJS
<script src="myJavaScriptGenerator.php?param=value" type="text/javascript"></script>
        hdJS;

If your JavaScript is static -- already written, not needing modification -- you can simply link it to your PHP-generated HTML page as usual:

        echo <<< hdJS
        <script src="myJavaScript.js" type="text/javascript"></script>
        hdJS;

Another variation involves a static JS script that's fed dynamically-generated variables by PHP. This isn't a shared variable space between the two languages but more like a communication channel between them:

        $frog = 'Hello';
        $dog = 'world';

        echo <<< hdJS
        <script type="text/javascript">
                var frog = "$frog";
                var dog = "$dog";
        </script>

        <script src="myJavaScript.js" type="text/javascript"></script>
        hdJS;

Of course you can generate entire blocks of JavaScript logic with PHP if need be, but I would suspect that in most cases you'd be better off feeding a few key variables to a static JS script.
_________________________________________

Most of the time you'll want your JS script to execute after the HTML finishes downloading, so that JavaScript will be able to find all the page elements in the DOM. Three ways of doing this are:

1) Use the window.onload method in a linked JavaScript script:

        window.onload = jsFunctionName;

        function jsFunctionName()
        {
                ...
        }

Notice the lack of parentheses in the onload statement. This is because you want to set window.onload equal to the content of the function, not execute the function as would occur if you wrote "window.onload = jsFunctionName();".

The window.onload statement wipes out any pre-existing onload function. Multiple linked scripts can use this method cooperatively this way:

        // save any pre-existing onload function
        var uniqueVariableName = window.onload;

        // do the following when the page finishes downloading:
        window.onload = function()
        {
// if there was a pre-existing onload function, run it now
                        if (uniqueVariableName) uniqueVariableName();

                // run the function
                jsFunctionName();
        }

        // here's the function you want to execute when the page has loaded
        function jsFunctionName()
        {
                ...
        }

In this example, "uniqueVariableName" is different for each script that uses it. I usually use the name of the JavaScript file as part of this variable name to ensure uniqueness.

Multiple onloads can also be implemented using the addEventListener method:
http://developer.mozilla.org/en/docs/DOM:element.addEventListener


2) Use the onLoad attribute in the body tag:

        <body onLoad="jsFunctionName();">

...where jsFunctionName() is a function in a linked or inline script.

I don't recommend this method because it mixes HTML with scripting more than is necessary. The more separate you keep them, the easier both will be to maintain.


3) Insert the function call in a script statement at the bottom of the HTML markup:

        ...
        <script type="text/javascript">
                jsFunctionName();
        </script>
        </body>
        </html>

Same argument against mixing HTML with scripting, although one could argue that this constitutes the same degree of mixing that occurs with the <script src= tag.

Regards,
Paul
--
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