#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    5
    Rep 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.)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Sydney
    Posts
    41
    Rep Power
    14
    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>
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    5
    Rep 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?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Sydney
    Posts
    41
    Rep Power
    14
    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!
  8. #5
  9. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    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";

    ?>
    The real n-tier system:

    FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

    Amazon wishlist -- rycamor (at) gmail.com
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Sydney
    Posts
    41
    Rep Power
    14
    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.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Posts
    5
    Rep 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!
  14. #8
  15. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    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.
    The real n-tier system:

    FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

    Amazon wishlist -- rycamor (at) gmail.com

IMN logo majestic logo threadwatch logo seochat tools logo