How to display image with javascript?

Share:
Display image using Javascript


You could make use of the Javascript DOM API. In particular, look at the createElement() method.

You could create a re-usable function that will create an image like so...

 function show_image(src, width, height, alt) {  
   var img = document.createElement("img");  
   img.src = src;  
   img.width = width;  
   img.height = height;  
   img.alt = alt;  
   // This next line will just add it to the <body> tag  
   document.body.appendChild(img);  
 }  

Then you could use it like this...

 <button onclick=  
   "show_image('http://google.com/images/logo.gif',   
          276,   
          110,   
          'Google Logo');">Add Google Logo</button>   

No comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();

Ads