title: How to copy vbscript to a function and run it when the body onload triggers
body: <p>I've inherited a site which is mostly vbscript/asp.  One page has &lt;% vbscript %> before the DOCTYPE HTML PUBLIC... line and runs before the page opens.  I need to get this to run even if the user presses the back button on another page to get there.  Presumably by placing it an onload function -   Is that correct?</p>
  
  <p>This is a shortened piece of the &lt;% %> code:</p>
  
  <pre><code>&lt;%
  if not (IsEmpty(Session("MM_Username"))) then
      Set checkSet = Server.CreateObject("ADODB.Recordset")
          checkSet.ActiveConnection = MM_CA_STRING
          checkSet.Source = "SELECT * FROM cpgdb.dbo_tbl_printing_tempstore WHERE username = '" &amp; Session("MM_username") &amp; "' AND addedtocart = 'NO'"
          checkSet.Open()
  
  end if
  %&gt;
  &lt;!DOCTYPE HTML PUBLIC...
  ...
  &lt;body&gt;
  </code></pre>
  
  <p>I think I need to get it to run like this:</p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC...
  ...
  &lt;body onload="runcode()"&gt;
  
  Function runcode()
      if not (IsEmpty(Session("MM_Username"))) then
          Set checkSet = Server.CreateObject("ADODB.Recordset")
          checkSet.ActiveConnection = MM_CA_STRING
          checkSet.Source = "SELECT * FROM cpgdb.dbo_tbl_printing_tempstore WHERE username = '" &amp; Session("MM_username") &amp; "' AND addedtocart = 'NO'"
          checkSet.Open()
  
      end if
  End Function
  </code></pre>
  
  <p>I've tried to move this code into the function by simply copying the &lt;% code %> and pasting it between the Function - End Function.  This doesn't work - the syntax looks incorrect.  Can someone please tell me why and what adjustments I need to make it work and also if the onload event will do what I need it to?</p>
  
  <p>Any help appreciated.</p>