Tech Tip: Find Text in Web Area
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: September 13, 2021
The 'Find' (cmd+F or ctrl+F) option is part of the web browser's tools. The 4D Web Area just renders the HTML page content so it does not have this browser tool.
The two methods below allow you to perform a 'Find' in the web area. This first method finds and highlights the first matching text on the page in the web area. Calling this method again will highlight the second match and so on. It accepts 2 parameters, the web area name and text to find.
//========================================= //METHOD NAME: Util_WAFindText //DESCRIPTION: FINDS AND HIGHLIGHTS MATCHING TEXT //PARAMETERS: // - $1 = WEB AREA NAME // - $2 = TEXT TO FIND //========================================= C_TEXT($1;$2;$WAName_t;$findString_t;$found_t) If (Count parameters=2) $WAName_t:=$1 $findString_t:=$2 $found_t:=WA Evaluate JavaScript(*;$WAName_t;"window.find("+Char(34)+$findString_t+Char(34)+");") End if |
This method returns the number of matches on the page for the searched string. It accepts 2 parameters, the web area name and text to find.
//========================================= //METHOD NAME: Util_WAFindTextCount //DESCRIPTION: FINDS NUMBER OF MATCHES //PARAMETERS: // - $1 = WEB AREA NAME // - $2 = TEXT TO FIND // - $0 = RETURNS COUNT OF MATCHING TEXT //========================================= C_TEXT($1;$2;$WAName_t;$findString;$textContent_t) C_LONGINT($0;$count_l;$matchPos_l) If (Count parameters=2) $WAName_t:=$1 $findString:=$2 $count_l:=0 $matchPos_l:=0 $textContent_t:=WA Evaluate JavaScript(*;$WAName_t;"document.body.innerText;") Repeat $matchPos_l:=Position($findString;$textContent_t;$matchPos_l+1) If ($matchPos_l#0) $count_l:=$count_l+1 End if Until ($matchPos_l=0) $0:=$count_l End if |
For example, we can call these two methods and search for the string "lorem". In the web area, the first occurance is highlighted and we see that there are 7 total matches on the page.
Util_WAFindText("WEB_AREA_NAME";"lorem") Form.count:=Util_WAFindTextCount("WEB_AREA_NAME";"lorem") |