HTML DOM JavaScript img src

Posted under javascript sample code On By mohammed.mutawe

HTML DOM JavaScript img src

One of the most important advantages of JavaScript is img src and as you know that HTML tags are many and varied in websites and there are complex sites that contain thousands of tags and because JavaScript is distinguished by it like other programming languages but in a special and different style we will introduce to you full tutorial about how to create JavaScript img src.

If we look at websites without images, we will find them somewhat boring and may contain a lot of words that are difficult to analyze without images Therefore, we will clarify the importance of having images on websites, namely:

  • Give a good aesthetic feel to the page.
  • Knowing and distinguishing topics easily because of the presence of Thumbnails that distinguishes them from others.
  • Facilitating the understanding of the explanation within the articles of any.


What is image property ?

JavaScript img src is an image rendered by JavaScript and it is a means by which this function is done but JS image property is an array containing all the images in a web page with all their properties and attributes.

For example, images.length reveals the number of images in the HTML page that appears in front of us, and since we are explaining the Image property, there are a number of functions that help us in this task, such as the nature of the images on the page and the extent to which browsers support these images and one of its most important functions is also to pass Images on the browser which provides a better way than other programming languages ​​or Backend languages ​​to manage browsers.


How to access?

As I mentioned to you, the image property helps us to control the browser images well and provide a good speed in navigating between pages within the framework of JavaScript language, and Before we start calling JavaScript img src we will first provide you with an example of how to access this type of code and this is done through:

if (document.images != null) { document.write(document.images.length); }


In fact, I don’t know why the browser showed me the presence of an image, Despite the print result was zero but it may have been adopted as a prepared array with an initial value of zero and if you well focused previously I checked whether there is an image or not in the array and instructed the browser to print the length and size of this array and when I executed the result I found that there are no images and this is normal because we did not provide the browser with anything from JavaScript img src.


Create JavaScript image

JavaScript is not very different from other languages ​​in adding images also It is not complicated at all and does not use complicated class in this task. Therefore, the first condition for adding an image in JavaScript is to define a variable and refer to it via hidden pointers with width and height, and imagine this code with me:

var _image = new Image(150 , 100);

All I did here is define a variable with the name of an image and I gave it two values, where the first value indicates the width of the image in pixels and the second value indicates the height of the image in pixels as well.

When selecting the image, you can cancel the width and height from the code so that the normal size is taken according to the image. Now that we have selected the image we will add the first JavaScript img src and the general formula for uploading and attaching images via the image property is done in this way:

if (document.images != null) {
 var _image = new Image(); 
     _image.src="your_img.gif"; //fill your url inside image source

I defined a variable name and you can express the variable names however you want, then I called the image property in the browser and set a condition and then I defined an image with the default measurements and finally I made a JavaScript img src and you can try this code on your own on your browser.


Preload images

When you preload, you make more than one image in the image array, which enables you to add many images to the page via JavaScript img src, and if you really want that, all you have to do is add more than one image with a different name and if you have more than 5 images, For example, you may be able to do hard coding inside an array, and if you make a loop, things will not differ much because there are no databases or files to facilitate the process.

Accessing JavaScript properties in this way is really fun, as it is one of the valuable tools that helps you to control the code very well. The most important property of the property is to use browser features that will not be available in languages ​​such as HTML and CSS.


Cons of much pictures

If you are a huge fan of uploading images on a site and your site contains many articles, you will face many difficulties, including:

  • Slow server when many visits from users.
  • Temporary service interruption.
  • The need to purchase a server with high premiums annually or even monthly.
  • Downloading and uploading many images on a specific article consumes page speed even with a giant server.
  • The need for a huge storage which can save all these pictures especially when you build social media website.



  • image property is a type of property from JavaScript.
  • You can use (document.image) function, which is an array through which you can access the number of images contained in your page’s content.
  • Preload is a good feature and helps you to speed up the access and loading of image content on your web page.
  • JavaScript img src expresses the actual file of the image, where the browser indicates the location of your image, which is either on your computer or on your website’s web hosting.



  • Partially referenced with Third Edition JavaScript Book.

Leave a comment

Your email address will not be published. Required fields are marked *