Thursday 25 October 2012

X-UA-Compatible Meta Tag Not Working

​You may or may not be aware that you can force IE to render a web page is a specific document mode, which means that an IE9 browser can be forced to render a page as if it was IE8 or IE7.  Why you'd do this is most likely due to the fact that forcing an newer browser to render like an older one (for instance, forcing IE9 to render like IE7) provides a convenient baseline when trying to style a page.

You do this by adding the X-UA-Compatible meta tag to the head block of a page as follows:

<meta http-equiv="X-UA-Compatible" content="IE=8" />

This particular line forces IE to render using IE8 mode.  You can read more about it here: http://msdn.microsoft.com/en-us/library/cc288325(v=vs.85).aspx.

There are a couple of caveats to adding this meta tag.  The first one is that it must be the first meta tag in the head block (there are a coupld of exceptions to this).  The second one is not so obvious, and is the main reason for this article.

If you use conditional comments to render the opening HTML tag of a page (see http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ for details about this) the meta tag just plain doesn't work.  It doesn't matter where you place it in the head block, it just doesn't work.  If you have the same issue, the way around it, as it turns out, is rather simple if a little weird: move it to before the conditional comments like so:

<meta http-equiv="X-UA-Compatible" content="IE=8"/>
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->

I've tested this and it appears to continue to work as expected with no downside (apart from ugly HTML).  Hopefully this will help someone out there.

Thursday 16 February 2012

Cookies...and not the munching kind

What am I talking about? Well, due to an amendment to the law (the Privacy and Electronic Communications (EC Directive) Regulations 2003, and the new Directive 2009/136/EC) in May last year, the way information is stored by technology is being changed, and primarily how cookies should and can be used and stored - hence the nick-name for this amendment: "The Cookie Law".

The UK government has given businesses a year to comply with the changes, which means that as from May 2012, any organisation that does business in the UK (and EU) is affected.

To find out more: http://www.ico.gov.uk/for_organisations/privacy_and_electronic_communications/cookie_rules_prepare.aspx

Just so you don't panic too much, this doesn't appear to affect intranets! It only applies to public-facing websites.

Wednesday 15 February 2012

Learning jQuery

If you ever wanted to learn jQuery but couldn't find the time or the discipline to do so, you can get some daily force-fed lessons by visiting http://learnjquery.tutsplus.com/ and signing up for the "30 days to Learn jQuery" course from Tuts+.

If you're starting out learning this stuff it should provide some structure (hopefully) to your initial learning.  Have fun.

Monday 30 January 2012

SharePoint Formatted String Control

I've come to the conclusion that a little known but widely used control, the FormattedString control in the Microsoft.SharePoint.WebControls namespace, is a very useful control. Any search through the Control Templates delivered with SharePoint shows how often this control is used when rendering output through SharePoint. It works in the same way a String.Format would in code, only it's something that can be added to an ASCX or ASPX asset.

Let's have a closer look.

In a publishing scenario, if you have some HTML into which you need to inject a value from the current page then you may be able to use the FormattedString control to create the HTML, as you might using String.Format. This can include HTML (i.e. "&gt;p&lt;" instead of "<p>") and any other server-side control output.

In a simple example, to do this you simply add in the usual "{0}" indexed tokens and then, as child controls, you add in the field values. Here's an example, after which I'll explain a little more:

<SharePointWebControls:FormattedString runat="server" 
FormatText="&lt;p&gt;{0}&lt;/p&gt;&lt;p&gt;{1}&lt;/p&gt;" 
EncodeMethod="NoEncode">
    <SharePointWebControls:FieldValue 
runat="server" FieldName="Title" />
    <SharePointWebControls:FieldValue runat="server" 
FieldName="Description" />
</SharePointWebControls:FormattedString>

The 2 child controls (FieldValue) render out the value of the 2 page field values as strings which are then used by the FormattedString control to replace the 2 tokens respectively. You could, as child controls, use any server control to output more complex examples should you wish, or your own custom controls. The render sequence always processes child controls first, so whatever the output is from the child control is used. You can have as many tokens as required. Another scenario might be to get the server-relative URL of the site, which you can do by using the SPUrl function in an ASP Literal control.

Going back to my example, the FieldValue control itself, is another little-documented control which I frequently use as it renders the value of a field without much or any of the surrounding HTML, which results in cleaner markup. Using it in the above context makes sense as we only want the actual values, not the additional HTML a control might output.

More information can be found here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.formattedstring.aspx and for the FieldValue control, some limited information can be found here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.fieldvalue.aspx.