title: Dynamically adding JS block in VB.NET code behind
body: <p>I am working on a system that administers a number of pages to end users for data entry.  There is a need to prevent the back button (I know, not ideal, but that is the requirement), but to disable this 'block' when viewing the pages in test mode.</p>
  
  <p>I have the necessary JS and it works for what we need, but I'm having issues adding the code dynamically to the page so that it fires.</p>
  
  <p>What do I mean by this?  Well, I can get the code block added dynamically so it <em>looks</em> exactly as it should, but the code doesn't work. If I copy and paste the text and manually add it to the aspx page (so not using code behind to do it), it works fine.  </p>
  
  <p>Here's the JS to work-around the back button click:</p>
  
  <pre><code>&gt; &lt;script type="text/javascript"&gt;
  &gt;       function preventBack(){window.history.forward();} setTimeout("preventBack()", 0); window.onunload=function(){null};
  &gt;     &lt;/script&gt;
  </code></pre>
  
  <p>In my <code>aspx.vb</code> code behind file I have the following:</p>
  
  <pre><code>If Not TestMode Then
    ' add JS
  End If
  </code></pre>
  
  <p>I have tried adding the JS in the following ways - none of which seem to have worked:</p>
  
  <ol>
  <li><p><code>Page.RegisterClientScriptBlock("preventBack", "&lt;script type=""text/javascript""&gt;function preventBack(){window.history.forward();} setTimeout(""preventBack()"", 0); window.onunload=function(){null};&lt;/script&gt;")</code></p></li>
  <li><p>Dim scriptText As New StringBuilder</p>
  
  <pre><code>scriptText.Append("&lt;script type='text/javascript'&gt;")
  scriptText.Append("function preventBack(){")
  scriptText.Append("   window.history.forward();} ")
  scriptText.Append("setTimeout('preventBack()', 0); ")
  scriptText.Append("window.onunload=function(){null};")
  scriptText.Append("&lt;/script&gt;")
  ClientScript.RegisterClientScriptBlock(Page.GetType, "noback", scriptText.ToString, False)
  </code></pre></li>
  <li><p>Dim lit As New Literal</p>
  
  <pre><code>lit.Text = "&lt;script type='text/javascript' src='JS/noBack.js'&gt;"  '"&lt;script type='text/javascript'&gt;function preventBack(){window.history.forward();} setTimeout('preventBack()', 0); window.onunload=function(){null};&lt;/script&gt;"
  Head1.Controls.Add(lit)
  </code></pre></li>
  <li><p>Dim gc As New HtmlGenericControl</p>
  
  <pre><code>gc.TagName = "script"
  gc.Attributes.Add("type", "javascript")
  gc.Attributes.Add("src", "JS/noBack.js")
  Head1.Controls.Add(gc)
  </code></pre></li>
  <li><p><code>ClientScript.RegisterClientScriptBlock(Me.GetType(), "MyScript", "JS/noBack.js", True)</code></p></li>
  </ol>
  
  <p>Where the noBackJS file contains the JS code mentioned above.</p>
  
  <p>As I said, the actually code is written to the HTML on the page when rendered, but doesn't work/fire at all.  Clearly I'm missing something here, but can't for the life of me work out what it is, can anyone help please?</p>
  
  <p>Thx.</p>