KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Loading JQuery in a Web Area
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: January 10, 2019

To execute JavaScript expressions in a Web Area, the commands like WA Evaluate JavaScript and WA EXECUTE JAVASCRIPT FUNCTION are very handy in working with loaded web pages in the Web Area. To take it a step futher with calling more JavaScript with these 4D commands, the following method can load JQuery in the Web Area:

// ----------------------------------------------------------------------
// Name: LOAD_JQUERY_IN_WEBAREA
// Description: Method will load JQuery library in a given Web Area
//
// Parameters:
// $1 (TEXT) - JQuery version in text (e.g. 2.2.1)
// $2 (POINTER) - Web Area form object pointer
// Output:
// $0 (LONGINT) - Status of loading in the Web Area
// 0 - Success
// 1 - JQuery version was not found
// 2 - Web Area was not loaded with a URL
// 3 - JQuery does not match library specified
// 4 - No JQuery loaded
// ----------------------------------------------------------------------
C_TEXT($1;$jQVersion;$jQversionR;$jQText;$url)
C_POINTER($2;$webArea)
C_LONGINT($0;$status;$response)

If (Count parameters=2)
  $jQVersion:=$1
  $webArea:=$2
  
  $content:=WA Get page content($webArea->)
  
  If ($content="")
    $status:=2 // Web Area URL was not loaded
  Else
    $url:="https://code.jquery.com/jquery-"+$jQVersion+".min.js"
    $response:=HTTP Get($url;$jQText)
  
    If ($response=200)
      WA Evaluate JavaScript($webArea->;$jQText)
      $jQversionR:=WA Evaluate JavaScript($webArea->;"jQuery().jquery")
  
      If ($jQversion=$jQversionR)
        $status:=0 // Success
      Else
        If ($jQversionR="")
          $status:=4 // No JQuery Loaded
        Else
          $status:=3 // Not the correct Jquery version
        End if
      End if
    Else
    $status:=1 // JQuery was not extracted
   End if
  End if
  $0:=$status
End if

Here is an example of loading JQuery 3.3.1 into the Web Area:

C_LONGINT($status)

$status:=LOAD_JQUERY_IN_WEBAREA ("3.3.1";->WebArea)


See Also: