KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Get Web Area's Web Rendering Engine
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: December 15, 2020

Below is a utility method to detect which web rendering engine is being used for a Web Area. the code only checks for the most frequently used browsers:

  • Internet Explorer
  • Safari
  • Chrome
  • Firefox
  • Apple Web Kit (MacOS)
// Method: Util_WA_GetWebEngine
// Description
// Returns a 4D Web Area's Web Engine based on
// the userAgent object
//
// Parameters
// $1 - Text : Name of Web Area object
// - Pointer : Pointer to Variable of Web Area object
//
// $0 - Object : Contains two properties
// {"Browser" : Name of Browser,
// "userAgent" : Full user agent string}
// ----------------------------------------------------

C_OBJECT($0)
C_VARIANT($1)

C_POINTER($waPtr)
C_TEXT($currURL)
C_TEXT($js_t)
C_OBJECT($res_ob)

If ((Value type($1)=Is pointer) | (Value type($1)=Is text))
   If (Value type($1)=Is text)
     $waPtr:=OBJECT Get pointer(Object named;$1)
   Else
     $waPtr:=$1
   End if

   $currURL:=WA Get current URL($waPtr->)

   If (($currURL=":///") | ($currURL=""))
     WA OPEN URL($waPtr->;"about:blank")
   End if

   $js_t:="var res={\"userAgent\":navigator.userAgent};"

   //Chrome
   $js_t:=$js_t+"if(navigator.userAgent.indexOf(\"Chrome\") > -1) "
   $js_t:=$js_t+"{ res.Browser=\"Chrome\"}"

   //Safari
   $js_t:=$js_t+"else if(navigator.userAgent.indexOf(\"Safari\") > -1)"
   $js_t:=$js_t+" {res.Browser=\"Safari\";}"

   //Internet Explorer
   $js_t:=$js_t+"else if(navigator.userAgent.indexOf(\"MSIE\") > -1 || "
   $js_t:=$js_t+"navigator.userAgent.indexOf(\"rv:\") > -1)"
   $js_t:=$js_t+" {res.Browser= \"IE\";}"

   //Firefox
   $js_t:=$js_t+"else if(navigator.userAgent.indexOf(\"Firefox\") > -1)"
   $js_t:=$js_t+" {res.Browser=\"Firefox\";}"

   //AppleWebKit
   $js_t:=$js_t+"else if(navigator.userAgent.indexOf(\"Macintosh\") > -1)"
   $js_t:=$js_t+" {res.Browser=\"AppleWebKit\";}"

   $js_t:=$js_t+"; res"

   $res_ob:=WA Evaluate JavaScript($waPtr->;$js_t;Is object)

   $0 := $res_ob
End if

Below is an example of using the command:
// "Web Area" is the name of the Web Area object
$currentEngine_t := Util_WA_GetWebEngine("Web Area")
or
// varWA is the variable of the Web Area object
$currentEngine_t := Util_WA_GetWebEngine(->varWA)

An example result:
{"Browser" : "Chrome",
"userAgent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"}

If the embedded web rendering engine is not being used, a method like this can help identify the engine and help with debugging issues or enforcing consistency due to differences in the engines.

Commented by John Foster on April 6, 2021 at 12:42 PM
Hi, In 4D v18.2 Mac (maybe other versions) this line will cause 4D to freeze: $res_ob:=WA Evaluate JavaScript($waPtr->;$js_t;Is object) Only fix is to force quit. Heads up! JOhn...