Forum Moderators: coopster

Message Too Old, No Replies

Converting JavaScript to PHP

Is there a PHP replacement for these variables?

         

Jeremy_H

8:34 pm on Jan 3, 2006 (gmt 0)

10+ Year Member



Hello,

I'm trying to convert one of my scripts from JavaScript to PHP to make it more accessible.

Most of it is if/else commands, which I've already been able to successfully convert. However, I have a few lines I'm having trouble converting.

Any suggestions? Thanks.

<script language="JavaScript">
var parentwin = external.menuArguments;
var doc = parentwin.document;
var sel = doc.selection;
var rng = sel.createRange();
var strhtml = new String(rng.htmlText);
alert(strhtml);
</script>

coopster

8:36 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



At first glance it looks as though this code is displaying in an alert window the text that a user has selected. Is that a true statement? I wouldn't want to assume anything here ;-)

If you might describe what this snippet of your code is doing we should be able to offer insight as to how to approach a solution ...

StupidScript

12:08 am on Jan 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like your snippet is, as coopster described, getting selected text from a document loaded into the browser and popping up a client-side Javascript alert() window.

PHP has no way of knowing what is happening at the client unless information is sent back to the server for it to digest.

Similarly, PHP has no handlers for the 'document' or 'window' object models. The 'window' is a reference to the client's browser window and the 'document' is a reference to the compiled file that is loaded into the client window, neither of which are available for PHP to deal with once the 'document' leaves the server.

Lastly, there is no PHP analogy to the Javascript alert() function, which is a client-side function.

Bummage ... ;)

topsites

5:21 am on Jan 6, 2006 (gmt 0)



Cheap, but it works:

echo "<script language=\"JavaScript\">\n";
echo "var parentwin = external.menuArguments;\n";
echo "var doc = parentwin.document;\n";
echo "var sel = doc.selection;\n";
echo "var rng = sel.createRange();\n";
echo "var strhtml = new String(rng.htmlText);\n";
echo "alert(strhtml);\n";
echo "</script>\n";