简体   繁体   English

Javascript .replace在特定服务器上失败

[英]Javascript .replace fails on specific server

I am having a problem with some Javascript which seems to work fine on my laptop when connected to localhost but not when the code is placed on a remote server and I connect to that from the same laptop and browser (IE11). 我遇到一些Javascript问题,当连接到localhost时,它似乎在我的笔记本电脑上可以正常工作,但是当代码放在远程服务器上并且我从同一台笔记本电脑和浏览器(IE11)连接到该Javascript时,它却无法正常工作。 The relevant code snippet is: 相关代码段为:

      var wktxt = inputs[i].getAttribute( "ondblClick" );
      wktxt = wktxt.replace("(" + rowno,  "(" + rowcnt)
      inputs[i].setAttribute( "ondblClick", wktxt );
      inputs[i].style.backgroundImage = "url()";  

It fails on the second line with "Object doesn't support property or method 'replace'" yet the problem is not hit locally and behaves precisely as intended (this is part of some logic that clones a row within an HTML table). 它在第二行失败,并显示“对象不支持属性或方法'替换'”,但问题并未在本地触发,并且行为精确如预期(这是在HTML表中克隆行的某些逻辑的一部分)。 When I initiate debugging on the failure, wktxt contains "function ondblclick() {AddNotes2(1,0) }", rowno is 1 and rowcnt is 7. 当我启动故障调试时,wktxt包含“ function ondblclick(){AddNotes2(1,0)}”,rowno为1,rowcnt为7。

Any ideas? 有任何想法吗? This code is executed within a loop - could it be anything to do with the var declaration being re-executed on each iteration? 这段代码是在循环内执行的-与在每次迭代中重新执行的var声明有什么关系吗?

Since the replace method is part of the string object prototype. 由于replace方法是字符串对象原型的一部分。 It would seem that the result of your var wktxt may not always equal a string. 似乎您的var wktxt的结果可能并不总是等于字符串。 To test this you can use the typof statement. 要对此进行测试,可以使用typof语句。 Something like 就像是

if (typeof wktxt === 'string') {
    wktxt = wktxt.replace("(" + rowno,  "(" + rowcnt)
    inputs[i].setAttribute( "ondblClick", wktxt );
    inputs[i].style.backgroundImage = "url()";
}
else {
    //write some code here to put whatever you want in wktxt should it fail to evaluate to
    // a string
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM