Webbrowser web page JS functions in Delphi call Delphi Functions

Source: Internet
Author: User

1.1 Activation
VaR doc, URL: olevariant;
Begin
URL: = 'about: blank '; // or a meaningful URL
Webbrowser1.navigate2 (URL); // This will activate it!
End;
1.2. write HTML code
VaR DOC: olevariant;
S: string;
Begin
DOC: = webbrowser1.document;
Doc. Clear; // clear the reason for writing new content
Doc. Write ('<HTML> ');
// Other code
Doc. Write ('Doc. Close; // This will take effect!
End;
1.3. Obtain HTML text
VaR DOC: olevariant;
S: string;
Begin
DOC: = webbrowser1.document;
S: =doc.doc umentelement. outerhtml; // The HTML text is displayed in S.
// Process S.
End;
2. Intermediate operations
2.1. Obtain the value of the HTML element.
VaR DOC: olevariant;
S: string;
Begin
DOC: = webbrowser1.document;
S: = Doc. All. BTN. value;
End;
2.2 rewrite the value of the HTML Element
VaR DOC: olevariant;
Begin
DOC: = webbrowser1.document;
Doc. All. BTN. Value: = '20140901 ';
End;
3. Advanced Operations
3.1. Events that trigger the HTML Element
VaR DOC: olevariant;
Begin
DOC: = webbrowser1.document;
Doc. All. BTN. onclick;
End;
3.2. Let the elements execute Delphi code other than webbrowser.
Originally, this had a very complicated solution, but that part of the ATL knowledge was relatively difficult to grasp, so it went around:
Let the HTML elements that need to execute Delphi call a function called triggerexevent. The parameter is the name of the HTML element, followed by several parameters.
Triggerexevent is a JavaScript function. There are not many parameters, but the first one must be the name of the element. Triggerexevent is a string prefixed with "# ontriggerexevent:" As the URL and then navigated.
In the onnavigator2 event of webbrowser, determine whether the URL contains "# ontriggerexevent:". How can this problem be solved if it contains:
A. Cancel this navigation; B. truncate the information after "# ontriggerexevent:" And pass it as a parameter to the ontriggerexevent event added to webbrowser. The parameters include: 1. Control name; 2. A string parameter.
In this way, you can process HTML click and other events in the ontriggerexevent event.
For example, I derived a new class for webbrowser called webbrowserex, which has an event called ontriggerexevent.
A js file contains such a function:
Function triggerexevent (cmpnt_id, event_nm, optionstr)
{Url = '# triggerexevent: Id = ';
Url = URL + cmpnt_id + '; eventnm =' + event_nm;
If (optionstr) url = URL + '; Params = optionstr ';
Location = URL ;}
You can write in HTML text as follows:
<Input type = "button" name = "Submit" value = "button" onclick = "triggerexevent ('submit ', 'click', null);">
In this way, when you click the button on the webpage, the code written by Delphi will be triggered. The Code will call other appropriate events based on the passed parameters, you can also enable webbrowserex to automatically find the appropriate event to trigger the event. Only when the event cannot be found ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.