0

I just want to bring image whenever i press css button but image is not coming. what can be wrong in this?

<html>
<head>
<script >
function create() {


var x = document.createElement("IMG");
x.setAttribute("src", "C:\Users\jai guru umesh\Desktop\webops\sha\fronthead images\rover0.png");
document.body.appendChild(x);




};

</script>
</head>
<body>
<input type="button" name="cssandcreate" id="cssandcreate" value="css"  onclick="create()"/>

</body>
</html>
5
  • It works OK for me jsfiddle.net/Br5xd/6 You should check the image url Commented Apr 19, 2014 at 14:01
  • It is saying file not found. why that is happening? any idea. Commented Apr 19, 2014 at 14:04
  • you don't have any image matching the name rover0.png in the specified path, I'm also not sure if the path is interpretable, normally we should use file:///C:/... not C:\... but I'm not really sure about that. Commented Apr 19, 2014 at 14:06
  • This will not work since the browser will not let you access files on your HD. You could load the image and hide and then on the onclick event just show it. And KingKong is correct, you need to use the file:/// protocol. Commented Apr 19, 2014 at 14:08
  • specified path is correct and in other cases it is working , but not in this. Commented Apr 19, 2014 at 14:12

3 Answers 3

1

Your error is in setAttribute function, you can't use this kind of path in web browsers as it works on client's pc and this would mean you could access their storage, which would be huge privacy concern.

You should use images locally from your file, either in same file as this html file, or in a subfolder, e.g. .../img (where ... is folder which contains html file). Then you can use relative path:

x.setAttribute("src", "img/rover0.png"); // this should work on any system

You can use / for windows paths too, but if you want to use \, make sure to use \\.

But if you insist on absolute file path, use this:

x.setAttribute("src", "file:///C:\\Users\\jai guru umesh\\Desktop\\webops\\sha\\fronthead images\\rover0.png");

Note the use of file:/// and \\ (using / instead \\ should work as well)

Sign up to request clarification or add additional context in comments.

3 Comments

Now how can I fix the position of the image?
@user2828274 how do you want to position the image?
i am using onmousedown to get the coordinates of my cursor and i am getting it correctly, now i want to create a image at the points wherever i will click in the screen. i was trying x.style.position=absolute; x.id='1'; x.style.left='c'; x.style.top='d';
0

You could load the image with the page and hide it until a user clicks on it.

<html>
<head>
<script >
function showImage(image) {



image.style.display = "inline";




};

</script>
</head>
<body>
<input type="button" name="cssandcreate" id="cssandcreate" value="css"  onclick="create()"/>
<img src="../fronthead images/rover0.png" style="display:none;" onclick="showImage(this)" />

</body>
</html>

Also I changed your path to a relative path since browsers will not allow access to your HD.

Comments

0

Use relative path (absolute if you have a link):

x.setAttribute("src", "../image.png");

In surplus, I suggest you to pay attention to the directory names (e.g fronthead images) it's better not use special characters and blank spaces (so "fronthead images", it's better in thi form "frontheadImages", or "fronthead_images")

2 Comments

How can i change position of the image?
You can use css (depends case by case) remember the difference between position types (relative, absolute and fixed). If you want to change the position with javascript i suggest to use jquery (.css method) that's the same to modify the css but by javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.