在 Javascript 中使用警报时出错(对象的属性“警报”不是函数)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11334581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error on using alert in Javascript (Property 'alert' of object is not a function)
提问by Idan Shechter
I am just trying to use alert and put a string variable inside the alert and get an error:
我只是想使用警报并在警报中放置一个字符串变量并得到一个错误:
Uncaught TypeError: Property 'alert' of object [Object Window] is not a function
My code is:
我的代码是:
var shortenurl = msg.d;
alert(shortenurl);
I've checked the value and it has a string inside, not an object.
我检查了值,里面有一个字符串,而不是一个对象。
回答by Chris Laplante
Somewhere in your code you overrode alert
. Check for var alert = ...
or some other kind of declaration like that. Also check for window.alert
declarations.
您在代码中的某处覆盖了alert
. 检查var alert = ...
或其他类似的声明。还要检查window.alert
声明。
回答by Lester
I had that error message due to an alert()
blocked by my pop-up-blocker.
由于alert()
我的弹出窗口阻止程序阻止,我收到了该错误消息。
回答by Rob Wilkins
I'm adding this one as an addition to this. In my case, when I had a similar problem, it turned out to not be my own code that was causing the problem but a poorly written extension that had been added to a client's browser. Once it was disabled, the script error went away.
我正在添加这个作为对此的补充。就我而言,当我遇到类似问题时,结果证明不是我自己的代码导致了问题,而是添加到客户端浏览器的编写不当的扩展程序。一旦它被禁用,脚本错误就消失了。
If you haven't overridden the method name in your own code anywhere, you may want to try disabling extensions to see if any of those is inadvertently interfering with your script.
如果您没有在自己的代码中的任何地方覆盖方法名称,您可能想要尝试禁用扩展以查看是否有任何扩展无意中干扰了您的脚本。
回答by WojCup
Check if you've got a Bootstrap .js declaration if required (after jQuery) i.e.
如果需要(在 jQuery 之后),请检查是否有 Bootstrap .js 声明,即
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
回答by Petro
Adding to Chris's answer... I had mistakenly overrode alert in my own function!
添加到克里斯的答案......我错误地在我自己的功能中覆盖了警报!
//Don't do this:
function alertOrConsole(node, alert){
if(alert){
alert(node);
}else{
console.log(node);
}
}
//----------------------------
//Fixed:
function alertOrConsole(node, flag){
if(flag){
alert(node);
}else{
console.log(node);
}
}
回答by smilyface
Mozilla says,
Mozilla 说,
The alert function is not actually a part of JavaScript itself.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
You can not see a function called alert here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
您在此处看不到名为 alert 的函数:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects