Simon Thomsen
Simon Thomsen

Reputation: 1421

How to show image through javascript file

I would like to know, how i can show a image through my javascript code.

Example:

javascript.php:

echo '<script type="text/javascript" src="http://domain.com/show_image.php"></script>';

show_image.php:

echo '<img src="http://domain.com/image/show_image.png" />';

This will not show anything. Anyone know why? :)

Upvotes: 0

Views: 1127

Answers (3)

Free Consulting
Free Consulting

Reputation: 4412

That should be:

echo 'document.write("<img src=\"http://domain.com/image/show_image.png\">")';

above assumes proper content-type for response

header('Content-type: text/javascript', true); 

Upvotes: 2

user210158
user210158

Reputation:

Assuming you want the <img> to appear where the <script> is, first change the <script> to something like this:

<script id="imagePlace" type="text/javascript" 
  src="http://domain.com/show_image.php"></script>

Then change show_image.php to this:

var scr = document.getElementById('imagePlace');
var img = document.createElement('img');
img.setAttribute("src","http://domain.com/image/show_image.png");
scr.parentNode.insertBefore( img, scr );

Upvotes: 1

alex
alex

Reputation: 490647

Because script elements are not designed to show images.

Use include 'show_image.php'.

Upvotes: 1

Related Questions