Solution 1 :

  1. Use relative path. If your html is in folder To-Students and your images in To-Students/images then use const imgSrcs = ['images/4.jpg', 'images/5.jpg', 'image/6.jpg']

  2. remove the defer on your script and have

    window.addEventListener("load",function() {
      const img = document.querySelector('img');
      img.src =imgSrcs[Math.floor(Math.random() * imgSrcs.length)];
    })
    

Solution 2 :

I think It’s because the javascript cannot access to C (because it needs that administrator accept the javascript)

Problem :

this is my js code

const imgSrcs = [
    'C:UsersobitoOneDriveDesktopTo-Students4.jpg', 
    'C:UsersobitoOneDriveDesktopTo-Students5.jpg', 
    'C:UsersobitoOneDriveDesktopTo-Students6.jpg'
  ];
  const img = document.querySelector('img');

  img.src =imgSrcs[Math.floor(Math.random() * imgSrcs.length)];

and this is my html

    <!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>JavaScript: DOM</title>
    <link rel="stylesheet" href="css5.css">
    <script defer src="ex5.js"></script>
</head>
<body>
    <figure id="header">
        <img src="#" alt="#">
    </figure>
</body>

</html>

i am trying to make this page everytime you reload it a random picture from the array pops up
this only worked for me if i try to link an image through a website url or somthing
but when i try to get images from my pc and use the folderpath to reach the image this never works
and i keep getting this msg in the console
tar5.html:1 Not allowed to load local resource: file:///C:/UsersobitoOneDriveDesktopTo-Students%06.jpg

any idea why this is happening?

Comments

Comment posted by legendary child

Thanks kind sir you helped me so much i swear ihave been trying for 2 days with no success haha thanks again <3

By