In cases like these it’s good to setup a bool variable and test whether you are first in edit mode or not – then proceed.
SharePoint 2013 and SharePoint 2010
The following call will return true if you are in edit mode, or false if you are in view mode:
Before your UI calls or UI functions – perform a quick logic test. If the value of InEditMode is false, proceed with the rest of the script: