2016/12/17

Javascript Add Photo

This post is about how to dynamically add a pic in a web page.



Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>

<html>
 <button onclick="add_pic();">Add pic</button>
<head> 
<script>

 function add_pic() {
        var src = "http://www.mastimania.net/images/logo.gif";
        show_image("http://www.mastimania.net/images/logo.gif", 276,110, "Pic"); 
  
    }

    function show_image(src, width, height, alt) {
        var img = document.createElement("img");
        img.src = src;
        img.width = width;
        img.height = height;
        img.alt = alt;
        document.body.appendChild(img);
    }

</script>
</head>

<body>

</body>

</html>

Result


Reference

http://stackoverflow.com/questions/5451445/how-to-display-image-with-javascript

http://jsfiddle.net/Bc6Et/

No comments:

Post a Comment