I am working on plugin for im client that will be show chatlog in QWebView. Plugin must support html templates. Now I’m trying to append new messages by call QWebElement.appendInside(‘new message’) and if in the template there is javascript source it does not work. For example template may be such type:
<!--MessageIn-->
<div class="space"> </div>
<div class="in MessageIn" id="oneShot">
<div class="tr">
<div class="bl">
<div class="br">
<p class="head">
<span class="time" id="time">%time%</span>
<span class="name" id="name">%name%</span>
</p>
<p class="content">
<span class="text" id="text"><script>getitall('%text%','%name%','%cid%','%base%',meldungsart[0]);</script></span>
</p>
</div>
</div>
</div>
</div>
<script type="text/javascript">animation1();</script>
Functions getitall() and animation1() won't be executed.
I can’t use QWebElement.evaluatejavascript() because i don’t know function names.
On the web browser menu click on the "Edit" and select "Preferences". In the "Preferences" window select the "Security" tab. In the "Security" tab section "Web content" mark the "Enable JavaScript" checkbox. Click on the "Reload the current page" button of the web browser to refresh the page.
You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.
In HTML, JavaScript code is inserted between <script> and </script> tags.
To invoke this function in the html document, we have to create a simple button and using the onclick event attribute (which is an event handler) along with it, we can call the function by clicking on the button.
The solution is to use JQuery. Look at this example
//QString add contains block of HTML code
QWebElement bodyElem = webView->page()->mainFrame()->documentElement().findFirst("body");
add = "var $bodyy=$('body');"
+ "$bodyy.append('" + add + "');"
+ "null;";
bodyElem.evaluateJavaScript(add);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With