admin管理员组

文章数量:1315362

Currently working on Loading image dynamic. If the custom image has the path it has to take the custom image not the default one if there is no custom image path are not available it has to load default image in the given ID.

As I am new to Javascript I am trying my below code.

var customImageurl = "custom_Image/web.png";
var defaultImageurl = "";
    function loadGraphics(){
        document.getElementById("loadImage").src = defaultImageurl;
    }
loadGraphics();
<div id="loadImage"></div>

It was not working kindly help me.

Thanks in advance.

Regards Mahadevan

Currently working on Loading image dynamic. If the custom image has the path it has to take the custom image not the default one if there is no custom image path are not available it has to load default image in the given ID.

As I am new to Javascript I am trying my below code.

var customImageurl = "custom_Image/web.png";
var defaultImageurl = "";
    function loadGraphics(){
        document.getElementById("loadImage").src = defaultImageurl;
    }
loadGraphics();
<div id="loadImage"></div>

It was not working kindly help me.

Thanks in advance.

Regards Mahadevan

Share Improve this question asked Dec 19, 2014 at 0:41 Mahadevan SivasubramanianMahadevan Sivasubramanian 871 gold badge3 silver badges12 bronze badges 4
  • show your html pls i want to check the img tag – AngularLover Commented Dec 19, 2014 at 0:46
  • You need to program your logic using ifs like in your description. – PM 77-1 Commented Dec 19, 2014 at 0:46
  • hi thanks @PM 77-1 can you please help me – Mahadevan Sivasubramanian Commented Dec 19, 2014 at 0:49
  • What exactly do you need help with? – PM 77-1 Commented Dec 19, 2014 at 0:51
Add a ment  | 

2 Answers 2

Reset to default 3

Your problem is that a div has no src attribute, as in your example you have <div id="loadImage"></div>

Your defaultImageurl is also empty.

The solution is to use an <img/> element:

var customImageurl = "custom_Image/web.png";
var defaultImageurl = "";
function loadGraphics(){
    //v-- will not work! 
    document.getElementById("loadImage").src = defaultImageurl;
    //v-- will work given your example conditions
    document.getElementById("loadImage").src = customImageurl;
}
window.onload = function(){
    loadGraphics();
};
<div id="loadImage-wrap">
    <img id="loadImage" />
</div>

You are trying to use a src attribute on a <div> element.

本文标签: Dynamic load image amp Custom Load image using javascriptStack Overflow