Jump to content

Javascript include


jwwceo

Recommended Posts

what you can do is call the php file on the other server through the src attribute of a script tag:

<script src="http://otherdomain.com/jscript.php"></script>

what that jscript.php file consists of is a bunch of javascript document.writes with php code mixed in with it:

<?php
    // jscript.php
    // set my mime header
    header('Content-type: text/javascript');
    // stepping out of php now
?>
document.write("myname=<?php echo 'test'; ?>");

--the general idea is that you mix in php with javascript to insert values recieved from php into the js.  Only js will actually be sent to the browser where it will then be executed and the document.writes will write out some html like a div in the page at that point.

 

This method is actually the method used by google for it's google gadgets and is the method used by many other web widgets.

Link to comment
https://forums.phpfreaks.com/topic/96905-javascript-include/#findComment-496201
Share on other sites

queries operate on the server side not the client side.  you can output the results of the query into the document.write statements:

<?php
    // jscript.php
    // set my mime header
    header('Content-type: text/javascript');
    // stepping out of php into javascript now
    // i'm gonna wrap everything in a div
?>
    document.write('<div>');
<?php
    // some open db code here
    $result = mysql_query($sql);
    // loop through result
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        // stepping out of php into javascript now
?>
document.write("rowvalue=<?php echo $row['columnname'] . '<br>'; ?>");
<?php 
    } // end while loop
    
     //close the div
?>
    document.write('</div>');

--the thing to understand is that the php executes on the server side and can do anything php normally does including accessing dbs.  However, just like when php is mixed with html, the php is used to variably output html and then that is sent to the browser and interpreted.  In this case you use php to variably output  javascript  which is then interpreted by the browser.

 

Because you are calling a php page on another server, ajax would not work for you and the only other option to doing what you want besides the above is to make an iframe in your calling page.

Link to comment
https://forums.phpfreaks.com/topic/96905-javascript-include/#findComment-496807
Share on other sites

yup , you're just using php to echo values into js just like you do with html.

 

if the js doesn't work as you expected, try entering the url of the script src attribute into your browsers address field directly and then you will be able to view source in your browser and see what code it's  actually returning and if it looks like valid js.

Link to comment
https://forums.phpfreaks.com/topic/96905-javascript-include/#findComment-497237
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.