Category Archives: JavaScript

JavaScript: Parse the Query String into a Usable Object

One common task when working in JavaScript on the client side is having to parse the query string into a usable object form. Here’s a function that returns an array of arrays representing the key/value pairs in the query string: … Continue reading

Posted in JavaScript | Leave a comment

JavaScript: Dynamically Add Options to an HTML Select Using jQuery

Here’s how you can dynamically add options to an HTML select using jQuery   var $selector = $(’#mySelect’); $selector.append($('<option>’, { value: "" }).text("– Choose –")); $.each(this.dataArray, function (idx, dataItem) { $selector.append($('<option>’, { value: dataItem.Key }).text(dataItem.Value)); }); $selector.append($('<option>’, { value: "All" … Continue reading

Posted in JavaScript, jQuery | Leave a comment

JavaScript: Extending String to Have startsWith and endWith Functions

Here’s two prototype functions you can add to your JavaScript to give you “startsWith” and “endsWith” functions: if (!String.prototype.startsWith) { String.prototype.startsWith = function (str) { return this.slice(0, str.length) == str; }; }   if (!String.prototype.endsWith) { String.prototype.endsWith = function(str) { … Continue reading

Posted in JavaScript | Leave a comment

jQuery: Global AJAX Error Handler

Here’s a quick and dirty way to handle AJAX errors globally if you are using jQuery:   $(document).ready(function () { $.ajaxSetup({ error: function (x, e) { if (x.status == 0) { alert(’Network error’); } else if (x.status == 404) { … Continue reading

Posted in JavaScript, jQuery | Leave a comment

JavaScript: Quickly Trim the Last Character from a String

When developing web applications, you undoubtedly have wanted to remove the last character from a string (such as a URL or application path)  in your JavaScript code. Here’s a quick way to get it done using the JavaScript function slice():   var … Continue reading

Posted in JavaScript | Leave a comment

JavaScript, UTF-8 Encoding: escape() vs. encodeURIComponent()

ASP.Net web applications default to UTF-8 encoding.  Now, if you are using JavaScript to create or change URLs, you might run into an issue with the way the function “escape()” encodes characters if you are handling the decoding in your … Continue reading

Posted in ASP.Net, C#, JavaScript | Leave a comment