JSONP jQuery Ajax Error Handler Callback

Are you using JSONP and jQuery and trying to handle non 200 responses? Getting frustrated that your error handler isn’t firing when it gets a 403, 404, or some other response code? Take a breath and repeat after me.

“There is no error handling in JSONP”

Now that you have fallen into a great despair, let me save you.

Head over to https://github.com/jaubourg/jquery-jsonp to find a good alternative to jQuery’s implementation of JSONP.

The solution was easy to implement and helped me solve the issues I was having with getting 403 and 404 errors on JSONP requests.

jQuery-JSONP is feature rich with

  • error recovery in case of network failure or ill-formed JSON responses,
  • precise control over callback naming and how it is transmitted in the URL,
  • multiple requests with the same callback name running concurrently,
  • two caching mechanisms (browser-based and page based),
  • the possibility to manually abort the request just like any other AJAX request,
  • timeout mechanism.

and compatible with all major browsers

  • Internet Explorer 6+
  • Firefox 2+
  • Chrome 1+
  • Safari 3+
  • Opera 9+

Photo by janet galore

IE8 & IE7 JavaScript Date NaN From String Error

When trying to create a JavaScript Date object from a string I was getting an NaN error in IE7 and IE8. My JavaScript worked in all the other browsers I was testing in. I was writing the script to figure out future dates based on the date the user selected. Issue is that IE7 and 8 don’t handle strings as well as real browsers. I was able to fix the error by using date.setFullYear instead of expecting the browser to handle it. Found a great function in StackOverflow.com to handle this.

 

function parseISO8601(dateStringInRange) {
	var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
		date = new Date(NaN), month,
		parts = isoExp.exec(dateStringInRange);

	if(parts) {
		month = +parts[2];
		date.setFullYear(parts[1], month - 1, parts[3]);
		if(month != date.getMonth() + 1) {
			date.setTime(NaN);
		}
	}
	return date;
}

You can check out my reduced test case.

Kynetx’s New Sandboxed Browser Extensions

I recently released my “Old School Retweet” Kynetx app in the Kynetx app store for the newly released browser extensions. I super love the new extensions and all that they do for users and developers alike. Something that I forgot when I released the app in the app store is that the new extension are sandboxed.

Because the extensions are sandboxed, all of the scripts from the extensions run a bit differently than they used to in the previous Kynetx extensions. Without getting into the technical details too much, the previous extensions just injected JavaScript into the page and the new extensions run JavaScript in a sandbox which has access to the DOM but can’t access anything else on the page. Because of this change my retweet app broke since I was using the jQuery loaded by Twitter.com to bring up the new tweet box (I do this because Twitter.com used that library to bind a click event and to trigger that event it has to be from the same library that bound it). Thankfully, with the help of a friend, I was able to get a work around for both Firefox and Chrome’s sandbox environment.

How I did it…

If the app is run not inside a sandbox I can just access the jQuery that Twitter.com loads to open a new tweet box

$("#new-tweet").trigger("click");

From within the Firefox sandbox I can access the page outside of the sandbox

window['$']("#new-tweet").trigger("click");

If I am in the Chrome sandbox I can create a script element that has the JavaScript that I want to execute. Crude, but it works. : )

var trigger_click_script = document.createElement("script");
var fallback = "window['$']('#new-tweet').trigger('click');";
trigger_click_script.innerHTML = fallback;
document.getElementsByTagName("head")[0].appendChild(trigger_click_script);

Here is the JavaScript code that I ended up with that gets executed when a user clicks on the retweet button.

// get stuff to retweet
var tweet = $K(this).parents(".tweet-content").find(".tweet-text").text();
var name = $K(this).parents(".tweet-content").find(".tweet-screen-name").text();

// build tweet
var retweet = "RT @"+name+" "+tweet;

// open new tweet box
$("#new-tweet").trigger("click");

// hack for FF sandbox
if ($("#tweet-dialog:visible").length === 0) {
  window['$']("#new-tweet").trigger("click");
}

// put tweet in new tweet box
$K(".draggable textarea.twitter-anywhere-tweet-box-editor").val(retweet).focus();
$K("#tweet_dialog a.tweet-button.button.disabled").removeClass("disabled");

// hack for chrome sandbox
if ($("#tweet-dialog:visible").length === 0) {
  var fallback = "window['$']('#new-tweet').trigger('click'); ";
  fallback += "window['$']('.draggable textarea.twitter-anywhere-tweet-box-editor').val('"+retweet+"').focus(); ";
  fallback += "window['$']('#tweet_dialog a.tweet-button.button.disabled').removeClass('disabled'); ";
  var trigger_click_script = document.createElement("script");
  trigger_click_script.innerHTML = fallback;
  document.getElementsByTagName("head")[0].appendChild(trigger_click_script);
}

Super Search Thingy Is Alive!!!

With a bit of free time I’ve had lately being unemployed, I was able to work on this project and finish it. I’m very happy to have it done and working so well.

What is it?

It’s a keyboard activated search tool for a specific website built to act like the application launcher Alfred App and QuickSilver. I went through each page in the Kynetx documentation site and indexed each page by hand. I didn’t include all pages which I believe makes this a much more powerful search for Kynetx developers. I also assigned key words to each page based on what I thought it should be searchable by. If you have suggestions, let me know because I would love to hear them.

Check out the live demo at http://supersearcher.michaelgrace.org/

If you like it, you are free to use the code in any way that you like. Cheers!