JavaScript / VBA automation: changing the onclick attribute
I am trying to modify a function call from a onclick
button event on a form using automation in VBA. The function is X
already hardcoded in the webpage and I have no access to modify this code. (The page is internal to my company, but I'm not in that department.)
When my scenario comes up, I want to replace the function X
with a new function Y
.
I have similar questions to this SO, but it seems to be more about changing an element's attribute from JS code. My problem is I need to do this in VBA and I'm not sure how to get there.
This site contains a lot of information about this process ( setAttribute
, getAttribute
etc.), including useful information re: caveats when working with one browser and another. All my code will interact with IE (which the company provides), so there is no need to worry about other browser options.
When trying to change an attribute onclick
, the VBE viewport displays the change for the attribute, but it does not behave as expected. After I change the attribute, nothing happens when I click (either through automation objButton.Click
/ objButton.onclick
or actually manually clicking the button). I have also tried this on other attributes (i.e. onfocus
) with the same results. The link above mentions that IE has some difficulty with this, but I am not very familiar with JavaScript and am having trouble with the manual.
I've tried the following, they all have the same result.
Option 1:
Set TEST = myIEWindow.Document.createattribute("onclick")
TEST.nodevalue = "function anonymous() {alert(""test""); };"
objButton.setAttributeNode TEST
Option 2:
objButton.Attributes("onclick").value = "function anonymous() {alert(""test""); };"
Option 3:
objButton.Attributes("onclick").value = "alert(""test"");"
Option 4:
objButton.setAttribute "onclick", "alert(""test"");"
Option 5:
objButton.onclick = "function() { alert(""test""); };"
While trying to use these different methods, I noticed that the onclick
type attribute in the VBE viewport changes from the original Variant / Object to Variant / String after trying to coerce to call the new function, and I feel that this has something to do with my problem.
Can someone please explain how (if possible) I can get this to work?
source to share