Jump to content

Javascript include


jwwceo

Recommended Posts

Hello!

 

I am trying to make a little widget type script for a sidebar in WordPress. I would like to include a DIV from one server onto another server

 

the source code is in php however.

 

Is there anyway to include php code as the source for a snippet of javascript??

 

James

Link to comment
Share on other sites

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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.