简体   繁体   English

Javascript function 如果从单独的文件中未定义?

[英]Javascript function not defined if from a separate file?

Here is my code: I am calling the function run() on the click of a button.这是我的代码:我通过单击按钮调用 function run()。 run() will call the function main() from the source code of bundle.js. run() 将从 bundle.js 的源代码调用 function main()。 The console logs that the function main() is not defined.控制台记录未定义 function main()。 I even tried running it in the console itself with no success.我什至尝试在控制台本身中运行它,但没有成功。


<script type="text/javascript" src="bundle.js"></script>

<script type="text/javascript">
  function run() {
    main(document.getElementById('playlistUrl').value)
  }
</script>


<input id="playlistUrl" placeholder="Enter a link...">
<button onclick="run()" id="go">Go!</button>

the function run() is defined, but not any funtion from bundle.js定义了 function run(),但没有来自 bundle.js 的任何功能

This is working fine,.这工作正常,。 check your file location or fails in bundle.js file检查您的文件位置或在 bundle.js 文件中失败

 //bundle.js file function main(value){ console.log(value); }

 //main.js file function run() { main(document.getElementById('playlistUrl').value) }
 <input id="playlistUrl" placeholder="Enter a link..."> <button onclick="run()" id="go">Go.</button> <script type="text/javascript" src="bundle.js"></script>

Code for index.html: index.html 的代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script type="text/javascript" src="bundle.js"></script>
</head>
<body>
  

<script type="text/javascript">
  function run() {
    main(document.getElementById('playlistUrl').value)
  }
</script>


<input id="playlistUrl" placeholder="Enter a link...">
<button onclick="run()" id="go">Go!</button>
</body>
</html>

Code for bundle.js: bundle.js 的代码:

function main(){
    document.write("Main ran")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未从单独的JavaScript文件NodeJS中定义require - require is not defined from separate javascript file NodeJS 当我将 Javascript function 从内联代码移动到单独的 js 文件时,出现“函数未定义” - I get "function is not defined" when I move a Javascript function from in-line code, to a separate js file 函数驻留在单独的文件中时,在javascript中未定义var - var not defined in javascript when function resides in separate file 在单独的 .js 文件中定义的访问函数与 .js 文件 - Access function defined in separate .js file from a .js file Javascript错误日志显示在单独的文件甚至同一文件中定义函数时未定义的函数 - Javascript error log says function not defined when I have defined it in a separate file and even the same file 无法调用在单独文件中定义的功能 - Unable to call function defined in a separate file 是否应该在单独的.js文件中定义网页的每个Javascript函数? - Is each Javascript function of a webpage supposed to be defined in its own separate .js file? 从Javascript调用PHP函数-PHP在单独的文件中 - Call PHP function from Javascript - PHP in separate file 在与第二脚本分开的脚本下定义的JavaScript中调用函数 - Calling a function in JavaScript defined under a separate script from a 2nd script 如何调用HTML中另一个文件中定义的javascript函数? - How to call javascript function defined in another file from HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM