Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

Wednesday, November 13, 2013

jQuery library loaded or not validation

Learning AngularJS; Guide for beginners:

If you are using jQuery on your page, then you should first validate that whether library file loaded or not because if your jQuery file is not loaded when you trying to run your jQuery code it will start throwing exception. So I would suggest you handle this situation instead of breaking page flow show your user a friendly message that required files are not loaded.

The reasons might be possible when your jQuery file not get loaded:

  1. Path referencing a jQuery file is not correct.
  2.  jQuery file name is renamed
  3.  If you are using CDN, and user has blocked your CDN path.
  4. Referenced CDN  server is down.
To handle this you should need to put a check before executing jQuery code, that validate whether file is loaded or not. If it is not available then you can prompt user the instructions to check or follow.

There are different ways to validate this:

if (window.jQuery) {  
    // jQuery library loaded  
} else {
    // jQuery library is not loaded
}

another way to check:

if (typeof window.jQuery == 'undefined') {  
    // jQuery library is not loaded
} else {
    // jQuery library is loaded
}


If  jquery.js library is not loaded, you can force to try again to load the file (but make sure you only using JavaScript as jQuery is not available at the time):

if (!window.jQuery || typeof window.jQuery == 'undefined') {
  // create script Element and append in head of HTML
  var jqElement = document.createElement('script'); 
  jqElement.type = 'text/javascript';
  // Path to jquery.js file, eg. Google hosted version
  jqElement.src = '/path-to-your/jquery.min.js'; //or reference an alternate CDN link
  document.getElementsByTagName('head')[0].appendChild(jqElement);
}

You might have noticed that we are only validating jQuery function being defined or not instead of $.

Reason is that this is a safe way to validate for jQuery file being loaded or not because you might using others libraries i.e, mootools.js then $ will conflict with these libraries. But if you are sure that you are not using others libraries that may conflict with jQuery then you can safely validate it by $ like:



if (typeof window.$ == 'undefined'){ 
  //file not loaded 
}

Similarly if you need to validate any jQuery plugin loaded or not you can easily validate it, as jQuery plugins are namespaces on the jQuery scope. Check that namespace for particular plugin exist or not:
if(window.jQuery().pluginName) {
     //run plugin dependent code
 }

If you want to add anything on this post please post your comments.

C# : JavaScriptSerializer and Time Zone Issue

Learning AngularJS; Guide for beginners:

Recently I was working on a module which include DateTime object. I found some interesting facts about this, which I want to share with my blog readers.I serialized DateTime object with c# JavaScriptSerializer, but when I deserialize the object it give me one day less from the date it get serialize



Example I serialized the date of 20th Jan 2012 date and assign it to a string, but when I deserialize the same string it give me the 19th Jan 2012



Here is code:

DateTime startDate=new DateTime(2012,1,20);//set the 20th of January
JavaScriptSerializer  serializer=new JavaScriptSerializer();
string serializeDate= serializer.Serialize(startDate);
DateTime afterDeserialize= serializer.Deserialize<datetime>(serializeDate);//I get 19th of Jan
Assert.Equals(startDate, afterDeserialize);

I tried to find out the reason posted it on StackOverflow and interestingly I found that actually It was not subtracting one day, but it converted the time-zone to UTC. So when it deserialized the DateTime object which was in PST(Pakistan Standard Time) time-zone, it was not in PST but in UTC. It's basically converted my PST DateTime object to UTC and then converted to tick like:

DateTime dateTimeObjectOfAnyTimeZone = /* incoming date */;
long ticks = dateTimeObjectOfAnyTimeZone.ToUniversalTime() // make UTC
  .Subtract(new DateTime(1970, 1, 1))     // subtract UNIX Epoch
  .TotalMilliseconds();     // get milliseconds since 1st jan 1970

// convert it into "\/Date(ticks)\/" format
string tickValue = String.Format(@"\/Date({0})\/", ticks);
return tickValue;

so on deserializing this string it gives me DateTime in UTC which was of 9 hours difference from my current time zone so subtracting 9 hours from 20th Jan 2012 12:00 AM converted it into 19th Jan 2012
So to get the correct output I tried:

// apply current TimeZone after deserializing tick using ToLocalTime()
// (apply the time zone).
DateTime afterDeserialize= serializer.Deserialize<datetime>(serializeDate);
afterDeserialize = afterDeserialize.ToLocalTime();


Now have the UTC time back to PST local time (with time zone applied)and it was my required 20th Jan 2012.
Life insurance policy, bank loans, software, microsoft, facebook,mortgage,policy,