Javascript从链接渲染图像

时间:2015-06-18 09:30:05

标签: javascript image hyperlink

我正在做一些事情,我的老板告诉我,我们的客户需要一个javascript,当放在他们的网站上将生成或渲染来自某个链接的图像。

示例链接。 http://i.imgur.com/YmmSEz9.png

这可能吗?我认为这是不可能的,因为你永远不会知道图像应该呈现的位置,但我的老板告诉我客户端会把它放在iframe中。然后iframe加载脚本,脚本将呈现图像。

1 个答案:

答案 0 :(得分:0)

完全可能:)

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
    // but you can adapt to make it append to the element you want.
    document.body.appendChild(img);
}

你可以这样使用它,例如:

<button onclick="show_image('http://i.imgur.com/YmmSEz9.png',  300,   250, 'Big Red Kangaroo');">Display image</button> 

(请参阅How to display image with javascript?了解代码的来源)

请看这个小提琴,看看结果:https://jsfiddle.net/gd9nfo32/4/