1

Taking image from the same folder the index and javaScript files are in, but for some reason it does not load. And the error it gives me is,

"Failed to load resource: the server responded with a status of 404 (Not Found)"
var img = dcoument.createElement("IMG");
img.height = 500;
img.width = 340;
img.src = '/img.jpg';

document.getElementById("addImgHere").appendChild(img);
1
  • 3
    Try img.src = './img.jpg'; or img.src = 'img.jpg'; Commented Jun 3, 2019 at 4:56

1 Answer 1

3

Your code works just fine.

You have two issues in your code:

  1. You have given image path as '/img.jpg'. It should be './img.jpg'
  2. You have spelled document wrong in line 1.

Incorrect:

var img = dcoument.createElement("IMG");

Correct:

var img = document.createElement("IMG");
Sign up to request clarification or add additional context in comments.

2 Comments

it's weird because before it worked without the dot in the image path but now the dot helped indeed and i only spelled document wrong here, sorry about that and thank you for your help :D
It is alright! './' is used for Relative Path and '/' is used for Absolute Path. You can google and read more about that. if you indeed want to use '/' , you would have to give the full path to the file from the root directory whereas you would use './' to give relative path to the file from the current working directory. But it is recommended to use Relative Path as it will work in all the environment wherever you deploy your site whether it's any server or localhost.

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.