Difference between revisions of "JavaScript"

From WeBWorK_wiki
Jump to navigation Jump to search
(added Problem Techniques category)
(added historical tag)
 
Line 1: Line 1:
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">It is not recommended to use javascript in a problem currently.</p>
  +
 
<h2>Javascript: PG Code Snippet</h2>
 
<h2>Javascript: PG Code Snippet</h2>
   

Latest revision as of 09:54, 28 June 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

It is not recommended to use javascript in a problem currently.

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