Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  127] [ 2]  / answers: 1 / hits: 6781  / 4 Years ago, sun, september 13, 2020, 12:00:00

I have a little html file that calls a javascript file, but when I'm trying to access it in the browser I'm getting the following error:



Access to script at
'file:///C:/Users/jekob/Desktop/battleship/index.js' from origin
'null' has been blocked by CORS policy: Cross origin requests are only
supported for protocol schemes: http, data, chrome-extension, edge,
https, chrome-untrusted.



I've googled that for hours and found out that I can host my app by a server (like node.js) and then to allow CORS. However I don't want any server. I want just a simple html and a js file.


index.html:


<!DOCTYPE html>
<html>
<head>
<title>battle-ship</title>
<link rel="stylesheet" type="text/css" href="index.css">

</head>
<body>
<div id="board"></div>
<script type="module" src="index.js"></script>
</body>
</html>

index.js:


import {board} from './board_0.1.js';

console.log(board);

board.js:


class cell{
constructor(){
this.locationByLetter = null;
this.locationByNumb = [];
this.occupied = false;
this.clicked = false;
}
}

class shipDitel{
constructor(name,size){
this.name = name;
this.size = size;
this.location =[];
}
}

export const board = buildBoard();
const shipType = [["Destroyer",2/*size*/],["Submarine",3],["Cruiser",3,],
["Battleship",4,],["AircraftCarrier",5]];

var shipsArr=setShip();
var selectedShip = selectShip(shipsArr[4]);
var stateGame ={
setting:true,
gameIsStart:false
}

function buildBoard(){
..
};

function setShip(){ //setting to a ship his name and size .
...
};

function selectShip(ship){
...
}

function onSelectedCell(cell){
...
};

function checkTheZone(cell){
...
};

More From » html

 Answers
1

I've googled that for hours and found out that I can host my app by a server (like node.js)



Yes



and then to allow CORS.



Since it will be Same Origin, you won't need CORS.



However I don't want any server. I want just a simple html and a js file.



Then you can't use browser-side ES6 modules.


You'll need to either write the code to not use modules in the first place, or use something like Webpack or Rollup to convert the code to not use modules afterwards.




There is a discussion in the HTML issue tracker about the motivation for the CORS requirement.


[#2689] Wednesday, September 9, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
louiemarvinm

Total Points: 473
Total Questions: 103
Total Answers: 94

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
louiemarvinm questions
Tue, Jan 4, 22, 00:00, 2 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
Mon, Dec 16, 19, 00:00, 5 Years ago
;