1

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,

1
  • 1
    Have you tried RunScriptFromFile instead of just RunScript...? Commented Nov 7, 2017 at 20:37

1 Answer 1

2

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);" &_   
    "})();"  
    
Sign up to request clarification or add additional context in comments.

3 Comments

Indeed it works ! Thank you so much man I would have never find this alone :)
Amazing what can happen when you read the Documentation.
@Lankymart, actually I tried it and looked in Chrome's developer tools and figured it out from there, Easton edited the documentation into this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.