set Qwebview url via js function



  • 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 "
    
        }
    }
    
    


  • Hi,
    [...]
    video = url;
    console.log(video);
    webView.url = video //webView is 'id' of your WebView{}
    [...]
    LA



  • @LeLev hey , how can i auto call the parsing() function i.e. when i load this qml the function should automatically get executed !!



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.