Quick tip: Disabling inline JavaScript within Sitefinity “edit” mode

by Mike Badgley on March 31, 2010

This is a handy little workaround I found for a small issue I was having within the CMS of Sitefinity. Pages that have some form of custom JavaScript functionality can sometimes play havoc with Sitefinity when in “edit” mode.

For example, I have a page that uses JavaScript to hide or show content based on options set from an image map. On page load, the content is set to be hidden, so when I go into Sitefinity to edit the page, the JavaScript functionality is getting run and thus all I get is an empty Generic Content control (where all my content has been placed). Not the biggest deal in the world, but it can be confusing for a client as they are not seeing the content that they are expecting to see. Further to my point, in this example there are a number of Generic Content controls on the page, so figuring out which one contained the content I wanted to modify was anybody’s guess!

That’s when I came up with this quick and easy workaround. Any JavaScript that you wish to be disabled within the Sitefinity CMS should be wrapped in the following if logic statement:

$(function() {

	if($('meta[name="Generator"]', 'head').size() > 0) {

		// Statements you wish to 'fire' when not in Sitefinity edit mode.

	}

});

On the public view of each Sitefinity-generated page, there is a meta tag created called “Generator” which contains the current version (value attribute) of Sitefinity that is being run, as shown in the following example:

<meta name="Generator" content="Sitefinity 3.7.2022.2:2" />

This meta tag does not get rendered when editing a page within Sitefinity. With this in mind, I simply check for it’s presence, and if it’s ‘found’, run the desired JavaScript statement. Otherwise, the JavaScript will not run and the content will be rendered as you (or more importantly, the client) would expect.

One comment

You could also check the querystring, like this:
http://www.sitefinitywatch.com/blog/10-01-28/Prevent_code_from_running_in_Edit_or_Preview_mode.aspx

by Chris on April 30, 2010 at 9:31 pm. #