1

I have some Google Plus One javascript in my .ASCX file:

<script type="text/javascript">
    (function () {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        po.src = 'https://apis.google.com/js/plusone.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
    })();
</script>

I need to hide this bit of code from IE7 and IE8 browsers.

What is the proper way to do this?

3 Answers 3

2

There are a number of ways to do this, if you are already including jQuery, you could use its browser sniffing functions. The other option is you could use IE conditional comments: http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx.

Sign up to request clarification or add additional context in comments.

Comments

1

Markup Method

<%  If Not (Page.Request.Browser.Browser.StartsWith("IE", StringComparison.OrdinalIgnoreCase) AndAlso Page.Request.Browser.MajorVersion < 9) Then%>
This is either Not IE, or IE 9 or greater<br />
<% end if %>

Code-Behind Method

Just Put the Script Inside PlaceHolder & Set Its visibility to true/false. Use to following function to detect browser in asp.net.

public static bool IsIE(Page page)
{
  if (page != null && page.Request != null && page.Request.Browser != null)
  {
    return page.Request.Browser.Browser.StartsWith("IE", StringComparison.OrdinalIgnoreCase) && (page.Request.Browser.MajorVersion == 7 || page.Request.Browser.MajorVersion == 8);
  }

  return false;
}

3 Comments

This is not a reliable way to do things. Many browsers have modes that 'emulate' IE and send IE User Agent strings. This will also hide the code from them as well.
If they want to emulate IE8 or IE7, then I do not think they should see code that is hidden from IE8 or IE7.
Also, taking a step back, why are you trying to hide this stuff from IE 7 and 8 users?
0
<!--[if !(IE 7)&!(IE 8)]>
You are not using IE7 or IE8
<![endif]-->

1 Comment

"You are not using IE7 or IE8" will not render on Firefox, Chrome, or Safari. I believe with this method, my code must include the script twice. Reference: quirksmode.org/css/condcom.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.