JavaScript

From WeBWorK
Jump to: navigation, search

Javascript: PG Code Snippet

This code snippet shows the essential PG code to include javascript in a WeBWorK question. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

It might be nice to include a different example than this here.

PG problem file Explanation
  loadMacros("parserNumberWithUnits.pl");

We don't need any changes to the tagging and description or initialization sections of the WeBWorK PG file to include javascript. This particular example, however, involves an answer with units , so we have to include the parser module for that.

  ## include javascript to open units
  ##    help window
  HEADER_TEXT(<<EOF);
  <script language="javascript" 
    type="text/javascript">
  <!-- //
  function unitspopup() {
      var url = "/webwork2_files/units.html";
      var opt = "height=625,width=600,location=no," +
                "menubar=no,status=no,resizable=yes," +
                "scrollbars=yes,toolbar=no,";
      window.open(url,'examdata_info',opt).focus();
  }
  // -->
  </script>
  EOF

In the problem set up section we can include any javascript that is to be inserted into the header of the web page that's created by using the HEADER_TEXT function. Here, we insert in the header of the file the javascript to open a popup window to display a units crib sheet.

  BEGIN_TEXT
  Enter 3 kg: \{ ans_rule(10) \}
  $BR
  (include 
  \{htmlLink("javascript:unitspopup()","units")\})

We can then use the function in the problem; here we include it as a link.

  ANS( NumberWithUnits("3 kg")->cmp() );

No changes are needed in the answer and solution section of the file, except any use of the javascript that one uses there. Here we don't have any changes.

Problem Techniques Index

follow us