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.");
开发者_JAVA技巧 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)
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>
<% } %>
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;
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
I know this was asked a while ago but hey-ho...
Why not use an <asp:Literal />
tag? As you can control the Visible
state of this.
The bonus of this is that no extra content is rendered (doesn't mess up if you need a <script>
in the <head>
tag) and you don't have any inline code.
Regards
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
精彩评论