[英]call javascript function located in main html from external js file
sorry this problem might be resolved already, but I have not been able to find the answer. 抱歉,此问题可能已经解决,但我无法找到答案。
I basically have 2 files: 我基本上有2个文件:
form.html code: form.html代码:
<!DOCTYPE html>
<html>
<head>
<script src=".../fileA.js"></script>
<title>BACKUP MONITOR</title>
<script>
function getEvents()
{
alert("getEvents");
}
alert("in HTML");
</script>
</head>
<body>
</body>
</html>
fileA.js code: fileA.js代码:
alert("in fileA");
getEvents();
Question 1: 问题1:
I can't call getEvents() from fileA.js. 我无法从fileA.js调用getEvents()。 Is there any way to do this?
有什么办法吗?
Question 2: 问题2:
Could I create a new Window from form.html and call getEvents() from there? 我可以从form.html创建一个新的Window并从那里调用getEvents()吗? please see code below:
请参见下面的代码:
New form.html code: 新的form.html代码:
<!DOCTYPE html>
<html>
<head>
<title>BACKUP MONITOR</title>
<script>
function getEvents()
{
alert("getEvents");
}
alert("in HTML");
var w = window.open();
var s = w.document.createElement("script");
s.type = 'text/javascript';
s.src = ".../fileA.js";
w.document.body.appendChild(s);
w.alert("New Window");
</script>
</head>
<body>
</body>
</html>
thank you very much 非常感谢你
Change the order in which your scripts load. 更改脚本加载的顺序。 Your external script
.../fileA.js
is loading before the getEvents
function is defined in your embedded script. 在嵌入式脚本中定义
getEvents
函数之前,正在加载外部脚本.../fileA.js
。 The call to getEvents
in your external script can't succeed if the function doesn't exist yet. 如果该函数尚不存在,则无法成功调用外部脚本中的
getEvents
。
form.html code: form.html代码:
<!DOCTYPE html>
<html>
<head>
<title>BACKUP MONITOR</title>
<script>
function getEvents()
{
alert("getEvents");
}
alert("in HTML");
</script>
<script src=".../fileA.js"></script>
</head>
<body>
</body>
</html>
Your code should be like this 您的代码应如下所示
<!DOCTYPE html>
<html>
<head>
<title>BACKUP MONITOR</title>
<script>
function getEvents()
{
alert("getEvents");
}
alert("1");
var eventArray = getEventArray();
var w = window.open();
alert("2");
</script>
<script src=".../fileA.js"></script>
</head>
<body>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.