[英]I keep getting a “Uncaught TypeError: string is not a function” error, even though my code seems to be fine. Can anyone have a look and explain?
<html>
<head>
<script language = javascript>
show = function()
{
document.getElementById("name").innerHTML = window.opener.game.pl.guild.n;
var app = document.getElementById("app");
var apps = window.opener.game.pl.guild.app;
for (a in apps)
{
var appAdd = document.createElement("option");
appAdd.text = apps[a];
appAdd.value = apps[a];
app.add(appAdd);
}
}
accept = function()
{
console.log("Accepting");
var app = document.getElementById("app");
window.opener.input.options = "acceptApp";
window.opener.input.an = app.options[app.selectedIndex].value;
}
</script>
</head>
<body onload = "show()">
<h1 id = "name" align = "center"> ### </h1>
<div style="text-align:center">
<p>You own this guild.</p>
</div>
</br>
<p>Applications:</p>
<select id = "app"></select>
<input type = "button" onclick = "accept()" value = "Accept application" />
</body>
</html>
Every time a user clicks the button, the accept function should be called. 每次用户单击按钮时,都应调用accept函数。 There's nothing wrong with the function itself, I'm sure about that.
功能本身没有任何问题,我确信这一点。 The function show that gets called when this starts just fills the selection with options, and create just sends it to the server.
该函数显示在启动时被调用只是用选项填充选择,而create只是将它发送到服务器。
You should put a semi-colon after the function call in your events: 你应该在事件中调用函数后放置一个分号:
onload = "show();"
onclick = "accept();"
Edit: I also see in these similar SO questions that there can be problems with function names that cause this exact error message. 编辑:我也在这些 类似的 SO问题中看到,可能存在导致此确切错误消息的函数名称问题 。 Just for fun, you might try renaming your functions...
只是为了好玩,您可以尝试重命名您的功能......
I have met the same question. 我遇到了同样的问题。 Then I figured it out that "download" is one of the reserved words of Chrome.
然后我发现“下载”是Chrome的保留字之一。 So I have to change the name of my javascript method.
所以我必须更改我的javascript方法的名称。
I think I know the root of the problem - I use AJAX to dynamically populate my page with HTML that is stored in variables. 我想我知道问题的根源 - 我使用AJAX用存储在变量中的HTML动态填充我的页面。 I had the exact same problem when I named my function the same as my variable, it would seem as if the browser was trying to run the variable as a function.
当我将我的函数命名为与我的变量相同时,我遇到了完全相同的问题,看起来好像浏览器试图将变量作为函数运行。 For instance:
例如:
var callthis = 'fjdashfkjdh';
function callthis() {
alert(callthis);
}
HTML: HTML:
<button onclick="callthis();" type="button">Click Me</button>
Clicking the button will cause the Javascript parser to run through the Javascript code, and when it hits the first line it runs the variable instead of seeking a function. 单击该按钮将使Javascript解析器运行Javascript代码,当它到达第一行时,它运行变量而不是寻找函数。 This is perhaps a bug in Javascript itself because it should look for the first function with the same name, not the first name which is equal to the function to be called eg
callthis();
这可能是Javascript本身的一个错误,因为它应该查找具有相同名称的第一个函数,而不是第一个名称,它等于要调用的函数,例如
callthis();
. 。
Hope this is helpful to someone. 希望这对某人有帮助。
I had this same problem when using variables with popular names. 当使用具有流行名称的变量时,我遇到了同样的问题。
The variable z, for example, is used by jQuery Mobile. 例如,变量z由jQuery Mobile使用。 If you are using the same variable name and using a jQuery mobile widget, your use of the variable will be overwritten.
如果您使用相同的变量名称并使用jQuery移动小部件,则将覆盖您对变量的使用。
Better to avoid globals, of course. 当然,最好避免使用全局变量。 Better still to choose variable names not likely to lead to conflicts.
更好的是选择不太可能导致冲突的变量名称。
Var zZz
for example. 例如,
Var zZz
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.