Results 1 to 4 of 4

Thread: add custom HTML attribute by javascript in QtWebKit

  1. #1
    Join Date
    Mar 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question add custom HTML attribute by javascript in QtWebKit

    Hi everybody,
    tody I use QWebView to load a html page, and want to add a custom HTML attribute "qlevel", someone told me QtWebKit doesn't offer DOM for HTML, so i want to use javascript to add this attribute, after adding the attribute, I want to export the HTML source, here is my code below:

    Qt Code:
    1. //---------add custom attribute "qlevel"
    2. QString strScript = "test.setAttribute = ('qlevel', '123');";
    3. QWebFrame* pFrame = m_pView->page()->mainFrame();
    4. pFrame->evaluateJavaScript(strScript);
    5.  
    6. //----------export the new html source
    7. strScript="alert(test.innerText.toString());";
    8. QMessageBox::information(this, tr("Test"), pFrame->evaluateJavaScript(strScript).toString());
    9.  
    10. QString strText = m_pView->page()->mainFrame()->toHtml();
    11. QFile file("c:\\test.html");
    12.  
    13. file.open(QFile::WriteOnly);
    14. QTextStream ts(&file);
    15.  
    16. ts << strText;
    17. file.close();
    To copy to clipboard, switch view to plain text mode 


    the "test" in javascript is an id of TD tag, now in the new HTML source code, I couldn't see the ‘qlevel’ atrribute, why? Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: add custom HTML attribute by javascript in QtWebKit

    Because the attribute is only added to the logic of the web, not to the source. This is a wrong way to go. You should get the source (maybe as you did or maybe using QHttp or QNetworkAccessManager) and once you have it, use Qt's text manipulation abilities to inject the tag in proper places. If you are sure the page is a proper xml file, you can use Qt's XML manipulation classes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: add custom HTML attribute by javascript in QtWebKit

    First, I am sorry to post the Thread here.

    I have solved this problem, because the javascript function's args must be quoted by double quotes. but remove the HTML property is still difficult. Now I want to use tidy to translate HTML page to XHTML and use QtXML module to manipulte HTML page.

    Thanks for your help!

  4. #4

    Default Re: add custom HTML attribute by javascript in QtWebKit

    Quote Originally Posted by sand.fj.wen View Post
    First, I am sorry to post the Thread here.

    I have solved this problem, because the javascript function's args must be quoted by double quotes. but remove the HTML property is still difficult. Now I want to use tidy to translate HTML page to XHTML and use QtXML module to manipulte HTML page.

    Thanks for your help!

    I am intrest in how to translate HTML page to XHTML? if html convert to xhtml how does the javacript look?
    Last edited by ChineseAlexander; 17th April 2009 at 07:31.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.