Difference between revisions of "JavaScript"

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Javascript: PG Code Snippet</h2> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This code snippet shows the essential PG code to include javascript in a ...)
 
(added Problem Techniques category)
(One intermediate revision by one other user not shown)
Line 6: Line 6:
 
<p style="text-align:center;">
 
<p style="text-align:center;">
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
  +
</p>
  +
<p>
  +
<em>It might be nice to include a different example than this here.</em>
 
</p>
 
</p>
   
Line 82: Line 85:
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
</p>
 
</p>
  +
  +
[[Category:Problem Techniques]]

Revision as of 16:34, 12 March 2008

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