Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get javascript working in call QWebElement.appendInside('some html code')?

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">&nbsp;</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.

like image 677
Sergey Avatar asked Oct 12 '12 08:10

Sergey


People also ask

Why JavaScript is not working in HTML?

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.

How do I use JavaScript and HTML together?

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.

Where do I write JavaScript code in HTML?

In HTML, JavaScript code is inserted between <script> and </script> tags.

How do you call a function in HTML?

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.


1 Answers

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);
like image 170
Sergey Avatar answered Oct 03 '22 08:10

Sergey