PDA

View Full Version : add custom HTML attribute by javascript in QtWebKit



sand.fj.wen
23rd March 2009, 07:13
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:



//---------add custom attribute "qlevel"
QString strScript = "test.setAttribute = ('qlevel', '123');";
QWebFrame* pFrame = m_pView->page()->mainFrame();
pFrame->evaluateJavaScript(strScript);

//----------export the new html source
strScript="alert(test.innerText.toString());";
QMessageBox::information(this, tr("Test"), pFrame->evaluateJavaScript(strScript).toString());

QString strText = m_pView->page()->mainFrame()->toHtml();
QFile file("c:\\test.html");

file.open(QFile::WriteOnly);
QTextStream ts(&file);

ts << strText;
file.close();



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!

wysota
23rd March 2009, 08:56
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.

sand.fj.wen
24th March 2009, 03:23
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!

ChineseAlexander
17th April 2009, 07:25
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?