By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,713 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,713 IT Pros & Developers. It's quick & easy.

accessing DOM

P: n/a
I want to access the DOM. Can something like this be done with PHP or a
combination of PHP/JAVASCRIPT? not tested!
<html><head><title>DOM test</title>
<?php
$inFileName = 'test.js';
$outFileName = 'jk.js';
function doIt() {
$dd = getElementById('first');
$dd.innerText=$outFileName;
}
?>
</head>
<body>
<p id="first" onclick="doIt()">ClickMe<br>
<p onclick="first.innerText=$inFileName">ClickMe<br>
</body></html>
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"J. J. Cale" <ph****@netvision.net.il> wrote:
I want to access the DOM. Can something like this be done with PHP or a
combination of PHP/JAVASCRIPT? not tested!
<html><head><title>DOM test</title>
<?php
$inFileName = 'test.js';
$outFileName = 'jk.js';
function doIt() {
$dd = getElementById('first');
$dd.innerText=$outFileName;
}
?>
</head>
<body>
<p id="first" onclick="doIt()">ClickMe<br>
<p onclick="first.innerText=$inFileName">ClickMe<br>
</body></html>


Of course you can do it, you just have to assure the PHP code will not be
parsed by your server and you have to find a browser which allows PHP, in
extension to javascript, as client side scripting language.

OTOH, if you want the client side javascript to call a server side PHP
function, you have to generate a completely new HTTP-Request using
document.location= or similar, resulting in a *completely* new page or in
*no* new page (for example HTTP 204: No Content).
If you want to *modify* the current page from within PHP using PHP on
javascript variables that cleary shows you didn't understand the concept of
a server side script language.
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #2

P: n/a
[original snipped]
If you want to *modify* the current page from within PHP using PHP on
javascript variables that cleary shows you didn't understand the concept
of
a server side script language.


With all due respect, this is of course, entirely possible. Perhaps before
you say that people do not understand, you should first make sure that you
understand it perfectly.

On first blush, you would seem correct: JavaScript is client side, PHP is
server side, and never the two shall mix. Yes, of course, you can use PHP to
write JavaScript as the page is rendered, and yes of course, this works
well.

But, think about this... place an inline frame (hidden) on the page to
modify the JavaScript variables from. Someone on your page is something that
the user does (presumably; perhaps they select an item from a drop down
list) that should cause a change. To make this happen, add a JavaScript
function that replaces the URL of the inline frame. The new URL loads a PHP
page which presumably does something (perhaps it gets data from a database).
As the page finishes, it writes a JavaScript function that calls back to the
opening page (the opener. syntax) and executes a function there; this
function can now happily change variables in JavaScript (because it IS
JavaScript). PHP can change JavaScript.

I will grant you it is not a straightforward approach. I will also tell you
that Apple has documented it quite well, and I have used it in production
for over two years without a hitch. In my experience (a nice way to say
things), this is a stable, reliable solution.

OK. Just my two cents.

david
Jul 17 '05 #3

P: n/a
david <so*****@microsoft.com> wrote:
[original snipped]
If you want to *modify* the current page from within PHP using PHP on
javascript variables that cleary shows you didn't understand the concept
of
a server side script language.
With all due respect, this is of course, entirely possible. Perhaps before
you say that people do not understand, you should first make sure that you
understand it perfectly.


Well... Maybe you have information I didn't know...
On first blush, you would seem correct: JavaScript is client side, PHP is
server side, and never the two shall mix. Yes, of course, you can use PHP to
write JavaScript as the page is rendered, and yes of course, this works
well.
That's how far I was in the statement.
But, think about this... place an inline frame (hidden) on the page to
modify the JavaScript variables from. Someone on your page is something that
the user does (presumably; perhaps they select an item from a drop down
list) that should cause a change. To make this happen, add a JavaScript
function that replaces the URL of the inline frame. The new URL loads a PHP
page which presumably does something (perhaps it gets data from a database).
As the page finishes, it writes a JavaScript function that calls back to the
opening page (the opener. syntax) and executes a function there; this
function can now happily change variables in JavaScript (because it IS
JavaScript). PHP can change JavaScript.


Ok, so far... But: It's not the PHP changing the JavaScript but the
JavaScript which was written by the PHP in the inline frame, sounds like
hair-splitting, but it *is* a difference. Further, you are not modifying
the *current* page (which is the one in the iframe and completely
reloaded), but the parent page which contains the iframe.

I know this seems obvious, but if you take the small technical details,
you'll see, that my previous posting perfectly matches your case, too.
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #4

P: n/a
Simon:
Well... Maybe you have information I didn't know...
I didn't know this either until I stumbled across one fine day. <grin>
Ok, so far... But: It's not the PHP changing the JavaScript but the
JavaScript which was written by the PHP in the inline frame, sounds like
hair-splitting, but it *is* a difference. Further, you are not modifying
the *current* page (which is the one in the iframe and completely
reloaded), but the parent page which contains the iframe.

I know this seems obvious, but if you take the small technical details,
you'll see, that my previous posting perfectly matches your case, too.


I have no wish to split hairs, and you are correct as well.

To me, it seemed the OP wanted the ability to affect JavaScript from PHP
(again, presumably because they are digging information out of a database or
other backend system/process, which I do not know how to do with just
JavaScript.); what I was suggesting does just that. The end result is that
PHP, um, adjusts JavaScript. It is just in a rather odd roundabout way. But,
I think the important thing is that the end user does not seem all the
backend stuff, and at the end of the day, that is the most important thing:
usability and a friendly page.

OK. Thanks for a good reply, Simon! I hope you have a most excellent day! (I
like it when everyone is right!)
david

Jul 17 '05 #5

P: n/a
> OTOH, if you want the client side javascript to call a server side PHP
function, you have to generate a completely new HTTP-Request using
document.location= or similar, resulting in a *completely* new page or in
*no* new page (for example HTTP 204: No Content).
If you want to *modify* the current page from within PHP using PHP on
javascript variables that cleary shows you didn't understand the concept of a server side script language.


You can actually do that in ASP.NET. You can attach server-side functions to
client-side events. You can also modify the document via the DOM on the
server-side.
Jul 17 '05 #6

P: n/a
On Sat, 25 Sep 2004 09:16:16 +0200, J. J. Cale wrote:
I want to access the DOM. Can something like this be done with PHP or a
combination of PHP/JAVASCRIPT? not tested!
<html><head><title>DOM test</title>
<?php
$inFileName = 'test.js';
$outFileName = 'jk.js';
function doIt() {
$dd = getElementById('first');
$dd.innerText=$outFileName;
}
?>
</head>
<body>
<p id="first" onclick="doIt()">ClickMe<br>
<p onclick="first.innerText=$inFileName">ClickMe<br>
</body></html>


DOM is a standards-defined XML API, not a Javascript feature. Many of
languages have DOM support, including PHP.

Be aware, however, that DOM support in PHP4 is not very good; I strongly
suggest to use PHP5 to use DOM, if possible.

Berislav
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.