The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Executing javascript from ermote php file
Discuss Executing javascript from ermote php file in the JavaScript Development forum on Dev Shed. Executing javascript from ermote php file JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 27th, 2001, 05:37 PM
|
|
Junior Member
|
|
Join Date: May 2001
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Executing javascript from ermote php file
I need to use a remote php script to execute some javascript. I'm using a line on my pages -
<script language="jscript" src="http://www.mydomain.com/file.php"></script>
How do I make the remote php script execute a javascript, or pass on the javascript.js file to the calling html document? (It is a bit like a cgi counter where the counter is linked to a cgi file which passes on an image, in this case I want to pass on a javascript file.)
|

May 27th, 2001, 05:53 PM
|
|
Contributing User
|
|
Join Date: Apr 2001
Location: Sydney
Posts: 41
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|
A php script will never execute javascripts. It's your browser that executes javascripts. You may use something like this;
<script language="jscript" src="http://www.mydomain.com/path/javascript.js"></script>
|

May 27th, 2001, 06:02 PM
|
|
Junior Member
|
|
Join Date: May 2001
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
No, what I want to do is use the php file to track the referring page and the number of visits. When this function is called from an html page -
<script language="jscript" src="http://www.mydomain.com/file.php"></script>
- the php file checks up some info from the mysql database and depending on that I want it to pass on different javascript (js) files to the html page. Then the browser will excute the js file. How do I do that?
|

May 28th, 2001, 08:04 PM
|
|
Contributing User
|
|
Join Date: Apr 2001
Location: Sydney
Posts: 41
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
Oh I see. I am not sure how you are going to do it from a plain html file unless you use SSI. But I don't believe the code;
<script language="jscript" src="http://www.mydomain.com/file.php"></script>
will execute the php file and pass the result to your browser.
Best approach maybe to use a php file instead of html. You can use "file" command to get the result from http://www.mydomain.com/file.php like;
$result=file("http://www.mydomain.com/file.php");
Good luck!
|

May 28th, 2001, 10:51 PM
|
|
Gödelian monster
|
|
Join Date: Jul 1999
Location: Central Florida, USA
|
|
Of course <script language="jscript" src="http://www.mydomain.com/file.php"></script> would execute PHP and pass the results back to the browser. Even though it is inside a <script> tag, that PHP code is being requested by the browser, so it will be parsed by the server first. (otherwise, we would have a big security hole, wouldn't we?)
Javascript and PHP operate in two completely different realms. PHP doesn't have anything to do with the browser, other than the fact that it can receive certain browser variables. It operates in server space only. Client-side Javascript operates in browser space only. So it's completely possible to have a script that is requested by the browser as a Javascript file, but is executed by the server first, upon which you can output any valid Javascript code you want, to be processed by the browser.
If for example, you want to conditionally use either one external .js file or another, depending on any combination of conditions you want (such as browser type or version), you could simply do:
PHP Code:
<?php
if(condition....etc...){ include("jsfile1.js"); }
elseif(condition 2 ...etc...){ include("jsfile2.js"); }
else{ include("jsfile3.js"); }
or, you could actually turn server-side variables into client-side ones, just by
PHP Code:
<?php
echo "variable1 = \"$variable1\";\n"; echo "variable2 = \"" . $variable2a . $variable2b . "\";\n";
?>
|

May 29th, 2001, 02:55 AM
|
|
Contributing User
|
|
Join Date: Apr 2001
Location: Sydney
Posts: 41
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
Rycamor, I don't think your first example will work. PHP will include whatever the comtents and possibly produces some error because of javascript syntax.
It may work if you echo the contents of js files.
Anyway, I learnt something. Thanks!! 
Last edited by sparki : May 29th, 2001 at 02:57 AM.
|

May 29th, 2001, 04:19 AM
|
|
Junior Member
|
|
Join Date: May 2001
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Hi Sparky,
The <script language="jscript" src="http://www.mydomain.com/file.php"></script> line is working perfectly well with the 'echo' command giving the contents of the js.
Hi Rycamor,
The include command was exactly what I was looking for. Hopefully I can use an external encoded js file now. Thanks!
|

May 29th, 2001, 10:00 AM
|
|
Gödelian monster
|
|
Join Date: Jul 1999
Location: Central Florida, USA
|
|
|
Sparki,
When PHP includes an external file, it drops out of PHP parsing mode for that file, unless the file has a <?php tag in it. So including Javascript will be absolutely no problem for PHP, the same as including HTML.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|