admin管理员组文章数量:1317131
I have set up a html page that contains a button that when clicked, activates a function from an external javascript file. The JS file in question imports two other functions from another JS file. However when I load the html file in my browser (Safari 12.0.2) and look at it in the debugger, I get an error stating "SyntaxError: Unexpected token '{'. import call expects exactly one argument."
The files are named "html_test_run.html", "test_run_javascript.js" and "export_test_run.js". I have tried setting the type of the script to "module" but that only resulted in a new error that said "Origin null is not allowed by Access-Control-Allow-Origin". I have also attempted to have three html tags, the first two had the source of the js files and the third defined a new function that the button would use instead, that didn't work either.
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Test run</title>
<meta name="description" content="Test run">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--
<link rel="stylesheet" href="">
-->
<script type="text/javascript" src="assets/test_run_javascript.js">
</script>
</head>
<body>
<button type="button" name="button" onclick="doSomething()">Click me</button>
</body>
</html>
First JS file:
import {doSomethingElse1, doSomethingElse2} from "export_test_run.js";
function doSomething(){
doSomethingElse1();
doSomethingElse2();
console.log("Test successful");
}
Second JS file:
function doSomethingElse1(){
console.log("Test successful1");
}
function doSomethingElse2(){
console.log("Test successful2");
}
export {doSomethingElse1, doSomethingElse2};
I expected the files would load correctly and the button would call the function "doSomething()" when clicked.
Any help would be greatly appreciated.
I have set up a html page that contains a button that when clicked, activates a function from an external javascript file. The JS file in question imports two other functions from another JS file. However when I load the html file in my browser (Safari 12.0.2) and look at it in the debugger, I get an error stating "SyntaxError: Unexpected token '{'. import call expects exactly one argument."
The files are named "html_test_run.html", "test_run_javascript.js" and "export_test_run.js". I have tried setting the type of the script to "module" but that only resulted in a new error that said "Origin null is not allowed by Access-Control-Allow-Origin". I have also attempted to have three html tags, the first two had the source of the js files and the third defined a new function that the button would use instead, that didn't work either.
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Test run</title>
<meta name="description" content="Test run">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--
<link rel="stylesheet" href="">
-->
<script type="text/javascript" src="assets/test_run_javascript.js">
</script>
</head>
<body>
<button type="button" name="button" onclick="doSomething()">Click me</button>
</body>
</html>
First JS file:
import {doSomethingElse1, doSomethingElse2} from "export_test_run.js";
function doSomething(){
doSomethingElse1();
doSomethingElse2();
console.log("Test successful");
}
Second JS file:
function doSomethingElse1(){
console.log("Test successful1");
}
function doSomethingElse2(){
console.log("Test successful2");
}
export {doSomethingElse1, doSomethingElse2};
I expected the files would load correctly and the button would call the function "doSomething()" when clicked.
Any help would be greatly appreciated.
Share Improve this question asked Jan 27, 2019 at 20:34 user3249872user3249872 672 silver badges4 bronze badges 3- This would help you: stackoverflow./questions/48754177/js-html-import-export – Batsheva Hansav Commented Jan 27, 2019 at 20:45
- @RichardSocker Thanks, but when I tried following the example and putting type="module" in the script tag and adding "./" in front of "export_test_run.js" and "assets/test_run_javascript.js" in the files test_run_javascript and html_test_run.html, respectively, I now get the error "Origin null is not allowed by Access-Control-Allow-Origin". Also I forget to mention that all of the files are on my HD, just in case that's causing the problem. – user3249872 Commented Jan 28, 2019 at 17:15
- @RichardSocker I would be interested in your suggestion. The reason that I use tow Javascript files is that the second file, "export_test_run.js", is a library (sort of) that contains functions that the file "test_run_javascript.js" (and eventually several other JS files) make use of. – user3249872 Commented Jan 29, 2019 at 15:49
2 Answers
Reset to default 5You have to correctly include a script on your page showing that the script type is "module":
<script src="/js/index.js" type="module"></script>
In case if the browser doesn't support modules you can simply process that using "nomodule" attribute:
<script nomodule>
console.info(`Your browser doesn't support native JavaScript modules.`);
</script>
You can include the two of your js files in the first page:
<script type="text/javascript" src="export_test_run.js">
</script>
<script type="text/javascript" src="assets/test_run_javascript.js">
</script>
Remove the export and import code from your js files! Use the correct paths for the including.
本文标签:
版权声明:本文标题:javascript - Error: "Unexpected token '{'. Import call expects exactly one argument" when tryi 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742019440a2414393.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论