Icon View Thread

The following is the text of the current message along with any replies.
Messages 11 to 15 of 15 total
Thread getElementById with HTML string?
Fri, Oct 2 2015 10:32 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

PA,

<< This works well. However, to follow your advice to not unnecessarily using external JavaScript, I tried to implement the functionality of the above Javascript in EWB: >>

The selectors haven't been added to the WebDOM unit's TDocument external interface.  I'll have to add them.

For now, as you found already, just use the GetElementByID.

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Oct 2 2015 10:44 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

PA,

<< Here the compiler did not complain. However, Opere gave me this error message: >>

In the WebDOM unit, add this method:

  external TDOMImplementation emit DOMImplementation = class
     public
        { Methods }
        function createDocument(const namespaceURI: String;
                                const qualifiedName: String;
                                doctype: TDocumentType): TDocument;
        function createDocumentType(const qualifiedName: String;
                                    const publicId: String;
                                    const systemId: String): TDocumentType;
        function createHTMLDocument(const title: String): THTMLDocument; <<<<<<<<<<<<<<<  Here
        function hasFeature(const feature: String; const version: String): Boolean;
     end;

I've added it for 2.02.  It was an IE9+ feature that didn't get added in for EWB 2.x.

And then your function should look like this:

function ExtractElementByIdFromString2(const HTMLString: String; const IdString: String): String;
var
TempDocument: THTMLDocument;
TempElement, TempElement2: TDOMElement;
begin
TempDocument := window.document.implementation.createHTMLDocument('Test'); // Create a temporary document
TempElement := TempDocument.createElement('div'); // Create a temporary element
THTMLElement(TempElement).innerHTML := HTMLString; // Set the passed string as HTML to the temporary element
TempElement2 := TempDocument.getElementById(IdString); // Find an element with the passed id
if Assigned(TempElement2) then
   Result := THTMLElement(TempElement2).outerHTML
else
   raise Exception.Create('Element not found with ID of "'+IdString+'"');
end;

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Oct 2 2015 12:49 PMPermanent Link

PA

Tim, thank you for the code.

However: The getElementById line does not work. HTMLString is:

<!DOCTYPE html>
<html>
<head>
   <title>ExtractDiv test</title>
   <style type="text/css">
       ul.c1 {list-style-type: upper-roman}
   </style>
</head>
<body>
   <p>Apples and oranges</p>
   <div id="main">
       <ul class="c1">
           <li>&#196;pfel</li>
           <li>Birnen</li>
       </ul>
   </div>
   <p>Men and women</p>
</body>
</html>

So you can see that the DOM element with the "main" ID DOES exist.
Fri, Oct 2 2015 1:34 PMPermanent Link

PA

PA wrote:

> However: The getElementById line does not work.

Maybe because the getElementById method is not yet implemented in the THTMLDocument class?
Fri, Oct 2 2015 4:22 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

PA,

<< However: The getElementById line does not work. >>

You (and I) didn't add the main div element to the document first.  Here's the correct version:

function ExtractElementByIdFromString2(const HTMLString: String; const IdString: String): String;
var
TempDocument: THTMLDocument;
TempBodyElement: THTMLElement;
TempElement, TempElement2: THTMLElement;
begin
Result:='';
TempDocument := window.document.implementation.createHTMLDocument('Test'); // Create a temporary document
TempElement := THTMLElement(TempDocument.createElement('div')); // Create a temporary element
TempBodyElement:=THTMLElement(TempDocument.getElementsByTagName('body')[0]);
if Assigned(TempBodyElement) then
   begin
   TempBodyElement.appendChild(TempElement); // Add the temporary element to the document
   TempElement.innerHTML := HTMLString; // Set the passed string as HTML to the temporary element
   TempElement2 := THTMLElement(TempDocument.getElementById(IdString)); // Find an element with the passed id
   if Assigned(TempElement2) then
      Result := TempElement2.outerHTML
   else
      raise Exception.Create('Element not found with ID of "'+IdString+'"');
   end
 else
   raise Exception.Create('No body element found');
end;

Tim Young
Elevate Software
www.elevatesoft.com
« Previous PagePage 2 of 2
Jump to Page:  1 2
Image