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)
SCRIPT tags have a negative impact on page performance because of their blocking behavior. While scripts are being downloaded and executed, most browsers won't download anything else.
The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.
You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags. The <script> tag alerts the browser program to start interpreting all the text between these tags as a script.
The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With