Javascript IE9 this=DispHTMLWindow2在原型内';s函数

Javascript IE9 this=DispHTMLWindow2在原型内';s函数,javascript,internet-explorer-9,Javascript,Internet Explorer 9,我在general js中创建了一个名为startsWith的扩展函数 事情是这样的: String.prototype.startsWith = function (str) { if (this.indexOf(str) == 0) { return true; } return false; } 在IE9中运行时,我得到一个错误:“对象不支持属性或方法'indexOf'” 在调试器中查看时,似乎这是DispHTMLWindow2 有什么帮助吗 谢谢。您可以尝试从 这在IE

我在general js中创建了一个名为startsWith的扩展函数

事情是这样的:

String.prototype.startsWith = function (str) {
 if (this.indexOf(str) == 0) {
     return true;
 }
 return false;
}
在IE9中运行时,我得到一个错误:“对象不支持属性或方法'indexOf'”

在调试器中查看时,似乎这是DispHTMLWindow2

有什么帮助吗


谢谢。

您可以尝试从


这在IE9中对我有效。你怎么称呼它?听起来它是作为函数而不是方法调用的。听起来你不是在传递字符串。如果(this.constructor!==String)returnRegExp对于
startsWith
endsWith
来说有点过分,您可以添加一个构造函数检查来阻止它出错。更不用说,如果
str
包含特殊字符,它们都会断开。
String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

String.prototype.trim = function(){return 
(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}