Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle the visibility of a script tag?

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)

like image 880
citronas Avatar asked Feb 12 '10 15:02

citronas


People also ask

Is script tag blocked?

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.

Why do script tag appear in body instead of head?

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.

Can script tags go anywhere?

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.

Does script tag go in body?

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.


2 Answers

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>
<% } %>
like image 114
SLaks Avatar answered Sep 23 '22 17:09

SLaks


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;
like image 34
Nick Craver Avatar answered Sep 22 '22 17:09

Nick Craver