I am trying to display image, through JavaScript, but i can't figure out how to do that. I have following
function image[a,b,c]
{
this.link=a;
this.alt=b;
this.thumb=c;
}
function show_image[]
{
document.write["img src="+this.link+">"];
}
image1=new image["img/img1.jpg","dsfdsfdsfds","thumb/img3"];
in HTML
I can't figure out where should I put something like image1.show_image[];
.
HTML? Or somewhere else...
Brett DeWoody
56.9k28 gold badges135 silver badges183 bronze badges
asked Mar 27, 2011 at 18:43
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 tag
document.body.appendChild[img];
}
Then you could use it like this...
Add Google Logo
See a working example on jsFiddle: //jsfiddle.net/Bc6Et/
dash
88.1k4 gold badges50 silver badges71 bronze badges
answered Mar 28, 2011 at 3:20
jessegavinjessegavin
72k27 gold badges137 silver badges164 bronze badges
4
Thank you... I made small modifications/simplifications. Works as intended.
HTML: Add Image
Javascript:
function addLogo[] { var src = "//i.gifer.com/origin/93/935d72c7bc35828ea93b5898691f28fd_w200.gif"; show_image[src, 124,124, "My Image"]; }
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];
}
answered Jul 28 at 18:06
HecanetHecanet
4014 silver badges3 bronze badges
Created: July-27, 2021 JavaScript is known as the web development language. By using JavaScript, we can make our web page attractive by inserting images into it. By default, we use the JavaScript has its own We will be creating a function called For example, Output: The above example displays an image on the webpage based on the provided source. The tag in HTML to display images. In the
tag, we have a method known as
src
, which helps get the source of the image that we gave to display. In the src
, we have to give the complete path of the image; otherwise, it will raise an error. The width, height of the image also needs to be
specified.img
tag, which helps us insert an image in our webpage. We can create a reusable function. Here we will be using the img
element again but differently.display_image[]
, which will consist of the parameters like source
, width
, height
, and alt
of an image in it. We will be using a createElement[]
for creating an element in the function. This way, we
can dynamically display images wherever we call this function.
function display_image[src, width, height, alt] {
var a = document.createElement["img"];
a.src = src;
a.width = width;
a.height = height;
a.alt = alt;
document.body.appendChild[a];
}
display_image['JavaScript.jpg',
276,
110,
'JavaScriptImage'];
document.body.appendChild[]
function adds the given element to the body of the webpage.