set Qwebview url via js function
-
wrote on 3 Aug 2017, 16:05 last edited by Qjay 8 Mar 2017, 16:21
Hello all , i want to set the url of webview using javascript function . the function uses regex to get the url from a web page and then i want to provide that url to webview .
below is the code
import QtQuick 2.0 import QtWebView 1.1 Item { property string video function parsing() { var http = new XMLHttpRequest(); var url; http.onreadystatechange = function(){ /* checks if the request finished and response is ready ( readystate 4) 200: "OK" */ if(http.readyState == 4 && http.status == 200) { var content = http.responseText; url = content.match(/"http:\/\/www\.youtube\.com\/embed\/.+\"/); console.log(url); } video = url; console.log(video); }; http.open('GET',"http://someurl/"); http.send(); } WebView { id: webView anchors.fill: parent url: "set url here " } }
-
wrote on 10 Aug 2017, 09:01 last edited by ODБOï 8 Oct 2017, 09:02
Hi,
[...]
video = url;
console.log(video);
webView.url = video //webView is 'id' of your WebView{}
[...]
LA -
Hi,
[...]
video = url;
console.log(video);
webView.url = video //webView is 'id' of your WebView{}
[...]
LAwrote on 10 Aug 2017, 12:15 last edited by@LeLev hey , how can i auto call the parsing() function i.e. when i load this qml the function should automatically get executed !!
-
wrote on 10 Aug 2017, 12:40 last edited by ODБOï 8 Oct 2017, 12:41
Use 'Component.onCompleted' handler to assign directly your url, or to call a function which will do that.
function setUrl(idOfWebView){
idOfWebView.url = "https://forum.qt.io"
}
WebView {
id: myWebView
anchors.fill: parent
// url: "set url here "Component.onCompleted : { /* any JavaScript function or expressions */ // myWebView.url = "https://forum.qt.io" ( DIRECT ) //or u can call a function like this : // setUrl(myWebView) // pass id of your webView to the function. ( FUNCTION ) } }
LA
3/4