views:

65

answers:

4

How do I toggle the visiblity of a

<script>

tag in the markup? Have the following javascript code in my master page:

<script  type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
  try {
   var pageTracker = _gat._getTracker("UA-108xxxx-2");
   pageTracker._trackPageview();
  } catch (err) { }
</script>
</body>

What is the best approach to serverside block this script from being rendered, if I want it to (like running in debug mode)

+4  A: 

Put it inside a server-side if block.

For example:

<% if (!Request.IsLocal) { %>
<script  type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
  try {
   var pageTracker = _gat._getTracker("UA-108xxxx-2");
   pageTracker._trackPageview();
  } catch (err) { }
</script>
<% } %>
SLaks
Thanks, I think I'll stick to this one ;)
citronas
+5  A: 

Try this:

<asp:PlaceHolder id="PHScripts" runat="server">
  <script  type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  </script>
  <script type="text/javascript">
    try {
     var pageTracker = _gat._getTracker("UA-108xxxx-2");
     pageTracker._trackPageview();
    } catch (err) { }
  </script>
</asp:PlaceHolder>

In your page:

PHScripts.Visible = !IsDebugMode;

For settings, I use a static class called AppSettings, and have a simple property like this beside the rest to determine if it's a debug build:

public static bool IsDebugMode
{
  get
  {
#if DEBUG
  return true;
#else
  return false;
#endif
  }
}

Then anywhere in the app:

AppSettings.IsDebugMode;
Nick Craver
I think you meant `PHScripts.Visible = !IsDebugMode;`
çağdaş
@çağdaş - Fixed, thanks! I'm normally showing more on debug if anything.
Nick Craver
+1  A: 

There are many ways to do this. One would be to add in a preprocessor directive, put your script in an external file and register the script from the code behind:

code behind:

protected void Page_Load(object sender, EventArgs e)
{
#if !DEBUG
    Page.ClientScript.RegisterClientScriptInclude("myScript", "/path/to/my/script.js");
#endif
}

The above assumes you moved your script to /path/to/my/script.js file....

Here's a good reference on the #if preprocessor directive and how to use it: http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx

KP
A: 

You might all be over complicating the problem.

Why not just and an ID and RunAt tag to the script:

<script id="DebugScript" runat="server">
var x = 1;
</script>

Then toggle it in the codebehind?

If bDebug Then
  DebugScript.visible = True
Else
  DebugScript.visible = False
End If
Axeva
I tried this one first, but somehow I got compile errors with the script I posted above.
citronas