0

I'm building a javascript website its a simple snake game but whenever I launch the index.html file it gives me this error

from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

my js files are:

       import { update as updateSnake, draw as drawSnake, SNAKE_SPEED } from './snake.js'
      let lastRenderTime = 0
      const gameBoard = document.getElementById('game-board')
    
      function main(currentTime){
        window.requestAnimationFrame(main) 
        const secondsSinceLastRender = (currentTime - lastRenderTime)/1000
         if ( secondsSinceLastRender < 1 / SNAKE_SPEED) return
    
         lastRenderTime = currentTime
    
         update()
         draw()
        }
    
        window.requestAnimationFrame(main)
    
        function update(){
          updateSnake()
        }
    
        function draw (){
            drawSnake(gameBoard)
        }

the other file:

export const SNAKE_SPEED =1
 const snakeBody = [{ x: 11, y:11 },
 { x: 10, y: 11 },
 { x: 12, y: 11 }
]
                    

 export function update(){
    //  console.log('update snake')
 }

 export function draw (gameBoard){
     snakeBody.forEach(segment => {
         const snakeElement = document.createElement('div')
         snakeElement.style.gridRowStart = segement.x
         snakeElement.style.gridColumnStart = segement.y
         snakeElement.classList.add('snake')
         gameBoard.appendChild(snakeElement)

     })
 }

I have looked up all other solutions but none has worked for me I used firefox and the same problem appeared

aman
  • 17
  • 4
  • What exactly do you mean by "*launch the index.html file*"? – Bergi Dec 10 '20 at 13:24
  • i meant when i opened the server locally I haven't deployed it yet – aman Dec 10 '20 at 15:19
  • How exactly do you do that? Are you opening a `file://`? Have a local http server? What domain do you use, `localhost`? What resources does your page include, how does it load them, what references does it use? Please post the html, the js does not appear very relevant. – Bergi Dec 10 '20 at 15:44
  • file:///C:/Users/SAMSUNG/Documents/snake_game/index.html this is the page im trying to open – aman Dec 10 '20 at 16:24
  • Yes, that won't work. You need an http server, including resources from the file system is not allowed. – Bergi Dec 10 '20 at 17:13

0 Answers0