This works ...
这有效......
html file ...
html文件......
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="src/myJS.js"></script>
</head>
<body onload="myJS();">
</body>
</html>
Contents of external javascript file (called myJS.js for convenience) ...
外部javascript文件的内容(为方便起见,称为myJS.js)...
myJS = function ()
{
document.write("Hello world");
};
But, this does not work ...
但是,这不起作用......
html file ...
html文件......
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="src/myJS.js"></script>
</head>
<body onload="myJS.myFunction();">
</body>
</html>
external javascript file ...
外部javascript文件...
myJS = function ()
{
myFunction = function()
{
document.write("Hello world");
};
};
Why not? Thanks in advance for any help.
为什么不?在此先感谢您的帮助。
2 个解决方案
#1
3
A function declared inside another function does not become a property of that function. If you want myJS to be an object with myFunction as a method you can do this
在另一个函数内声明的函数不会成为该函数的属性。如果您希望myJS成为myFunction作为方法的对象,则可以执行此操作
myJS = {
myFunction: function()
{
document.write("Hello world");
}
};
#2
0
your script creates two global functions...
你的脚本创建了两个全局函数......
so the myJS
creates another function called myFunction
either of which can be called independently.
所以myJS创建了另一个名为myFunction的函数,其中任何一个函数都可以独立调用。
looks like you want to make a JSON object like
看起来你想要制作一个JSON对象
myJS = {
myFunction: function() {
document.write("Hello world");
}
}
#1
3
A function declared inside another function does not become a property of that function. If you want myJS to be an object with myFunction as a method you can do this
在另一个函数内声明的函数不会成为该函数的属性。如果您希望myJS成为myFunction作为方法的对象,则可以执行此操作
myJS = {
myFunction: function()
{
document.write("Hello world");
}
};
#2
0
your script creates two global functions...
你的脚本创建了两个全局函数......
so the myJS
creates another function called myFunction
either of which can be called independently.
所以myJS创建了另一个名为myFunction的函数,其中任何一个函数都可以独立调用。
looks like you want to make a JSON object like
看起来你想要制作一个JSON对象
myJS = {
myFunction: function() {
document.write("Hello world");
}
}