从 VBS 调用的 Javascript 代码在运行时抛出未指定的错误

标签 javascript internet-explorer vbscript hp-uft

我有一个网页,我想在上面添加 jQuery 库,代码如下:

var script = document.createElement('script');
script.src ="https://code.jquery.com/jquery-1.11.3.min.js";
document.body.appendChild(script);

此代码是使用 VBS 代码从 UFT 调用的:

script= "var script = document.createElement('script');" &_
        "script.src =" & chr(34) & "https://code.jquery.com/jquery-1.11.3.min.js" & chr(34) & ";" &_
        "document.body.appendChild(script);"
Browser("myBrowser").Page("myPage").RunScript(script)

当我运行我的 VBS 代码时,抛出一个错误

"Erreur non spécifiée" | in English "unspecified error"

但我无法弄清楚为什么会抛出此错误,因为 VBS 和我的 js 代码似乎都正常...我正在使用 IE 8 企业模式并且在安全选项中启用了脚本。 我对如何解决这个问题没有更多想法...

谢谢,

最佳答案

UFT's online documentation 涵盖了这一点:

When running this method, you should use an eval() function or anonymous function to run the script entered for this method. For example, you can use functions like these:

"eval(var remove = document.getElementsByTagName('a')[0]; var per = remove.parentNode; per.removeChild(remove););"

OR "(function(){var remove = document.getElementsByTagName('a')[0]; var per = remove.parentNode; per.removeChild(remove);})();"*

请查看修改后的代码,该代码可以进行一些更改

  1. 代码放在 anonymous function and immediately executed (IIFE)
  2. 我使用单引号而不是 chr(34)(这没有区别,只是更具可读性)。

    script = "(function(){" &_ 
      "var script=document.createElement('script');" &_  
      "script.src='https://code.jquery.com/jquery-1.11.3.min.js';" &_  
      "document.body.appendChild(script);" &_   
    "})();"  
    

关于从 VBS 调用的 Javascript 代码在运行时抛出未指定的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159152/

相关文章:

javascript - IE8 javascript 检测为 IE7

php - jquery 翻转在 IE 中不起作用

c# - Internet Explorer 插件和网站

Git 钩子(Hook)预提交错误 : "command not found"

javascript - 如何选择具有 id 的特定按钮?

javascript - 当事件处理程序从另一个事件处理程序被 react 和减少时附加到组件上

javascript - 仅当窗口宽度调整大小时才调用 jquery 函数

javascript - 当我点击两次时,css3 转换不起作用

windows - AutoIT & VBS - 从 Windows 锁定屏幕将 PC 添加到域

r - 从 R 启动 VBA 宏