LostReality
LostReality

Reputation: 687

Javascript code called from VBS throwing unspecified error at runtime

I have a webpage on which I want to add jQuery lib, here is the code :

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

This code is called from UFT using VBS code :

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)

When I run my VBS code, an error is thrown

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

But i can't figure out why this error is thrown since both VBS and my js code seem OK... I am using IE 8 Enterprise Mode and script is enabled in security options. I have no more ideas on how to work around this issue...

Thanks,

Upvotes: 1

Views: 205

Answers (1)

Motti
Motti

Reputation: 114685

This is covered by 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);})();"*

Please see the modified code that works with a couple of changes made

  1. The code is placed in an anonymous function and immediately executed (IIFE)
  2. I use single quotes instead of chr(34) (it doesn't make a difference, just more readable).

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

Upvotes: 2

Related Questions