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 = [
  const img = document.querySelector('img');

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

and this is my html

    <!DOCTYPE html>

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


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?


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