0

I am using a WP template that allows me to incorporate arbitrary HTML. Unfortunately, I have to use this particular widget and can't use other WP widgets.

I have on my webserver /some/path/serve_image.php that spits out a random HREF'd IMG SRC with a caption and some other info from a MySQL query.

Now...how can I say "take that output and treat it as HTML"? If I just put "/some/path/serve_image.php" I get that literal string.

I tried:

<script type="javascript" src="/some/path/serve_image.php"></script>

but that didn't work. I tried changing everything in serve_image.php to be document.write() calls and that didn't seem to work either. I'm not the world's greatest JS guy...

So if I have a URL on the net that spits out some HTML and I want to include that HTML in my web page, what's the best way to do that? Sort of like what Google does with Adsense - you source their show_ads.js.

1
  • Are you using plain JavaScript or a framework like jQuery? Commented May 18, 2011 at 3:32

2 Answers 2

1

Why no? Add

header('Content-Type: application/javascript');

And output JavaScript Like:

echo("var image = \"".$images[array_rand($images)]."\";");
echo("$('img.randim').attr('src', image);
Sign up to request clarification or add additional context in comments.

Comments

0

No. JavaScript and PHP are two completely separate languages. In fact, if it was JavaScript, you aren't even loading it the right way.

<script type="text/javascript"></script>

The way you're trying to do it would throw a parse error, because it would try to use the PHP as JavaScript. Some browsers would even reject it, because PHP files have a text/html MIME type, while JavaScript should be application/javascript.

PHP has to be done server side, so loading it in the client just doesn't work.

What I think you're after is this:

<?php

    require('/some/path/serve_image.php');

?>

Just place that wherever you want the image to be.

8 Comments

That won't really work, because it needs to be placed in a certain spot in the page. But forget for the moment that it's PHP. What if it was just HTML? Is there a way to have a JS that says "source this URL over here and print out the HTML it produces"?
@raindog: You can move the require statement to wherever you want it. for a JS solution, load up a framework or break out some cross-browser XHR skills.
The MIME type issue isn't quite valid. You can use PHP files to output JS just fine. If you're worried about MIME types, all you need to do is set a header('Content-Type: application/javascript');, and the browser won't know the difference between the PHP file and a normal JS file. However, you obviously do need to output valid JavaScript, so you're not totally off :)
@yc Just did some testing. Chrome doesn't care for the MIME type.
@tylermwashburn : PHP has to be done server side, so loading it in the client just doesn't work... no one said that it wants the php to be compiled on the client, it just needs the outputted result(after the server side part is done)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.