How to invoke a javascript function in web page by injected javascript



Anybody can help on this? I injected a segment of javascript and try to invoke a javascript function in web page, and failed. To simplify the issue, I made a sample html page as below:

<script type="text/javascript">
var myFunc = function(){
	alert("Function has been invoked");
<a href="#" id="myTestHref" onclick="myFunc();">Click Here!</a>

When I injected below js, it works as my expectation.

However, when I tried to directly invoke the function: myFunc() with below js, it failed and error message is: “Message: ReferenceError: myFunc is not defined”.

I packaged sample and uploaded here: (2.1 KB)


This is awful way to write code, because of dirty global scope, but anyway, try to call window.myFunc()


This smart and i think this is very nice and useful one.


Thanks for your reply. Unfortunately, it does not work. Error message: “TypeError: window.myFunc is not a function”.
Anyway, I got a solution. In my actual practice, click a button on web page will invoke the js function. My difficulty is: the button’s element ID is dynamatically generated and uiPath can not capture it. As an alternative solution, with injectJS, I iterate all buttons and find out the correct one, then, invoke click() method.The injected JS likes:

	var btnCtrls=document.getElementsByTagName('A');


Calling stopRefreshSite() from javascript console stops the timed refresh from occurring.

I’d like to disable this recurring refresh with a chrome extension. I thought that a content script which calls stopRefreshSite() whenever the page loads makes sense, though because content scripts execute in an isolated world this doesn’t seem to be possible.

Is there any way to call the webpage function? Or would trying to prevent the original javascript from running in the first place be a better strategy?