PDA

View Full Version : JS not executes in QTWebkit



ericzhang
18th March 2014, 09:59
<html><body>
<a href="http://google.com" id="alink">link</a>

<script>
document.getElementById("alink").click();
</script>

When I load a page in QTWebkit with such content, the page doesn't jump to href location "http://google.com". It can jump with mouse click on the label A.

I think it's the problem of the JS function "click".

Can anyone help me to solve the problem?

Regards,
Eric

Added after 52 minutes:

I think it's because QTWebkit not support the JS function click. After add below JS code in html page, it works.
My problem is how to add the JS in any page automatic for all pages request.

<script type="text/javascript">
if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click)
HTMLElement.prototype.click=function(){
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt); }
</script>

wysota
20th March 2014, 18:23
You can probably use jQuery for that.

ericzhang
25th March 2014, 10:21
Yes, use jQuery can help. The problem is how to change the html content to add jquery to the page when QTWebkit load a frame.

wysota
25th March 2014, 12:31
I think you could just evaluate jQuery script with evaluateJavaScript(). jQuery should then add itself to the document and then you can start calling its functions.