Parsing JSON using jQuery

Posted on : 05-10-2010 | By : Sam Edwards | In : Developers, Frameworks, Tips

Tags: , ,

0

When retrieving data from external sources, it is often necessary to take the result of an AJAX call and create a JavaScript Object from it. This can be done by running your JSON string through eval to get back the resulting object. This greatly increases the risk of code injection into your site though, so parsing the JSON text is the preferred method.

The easiest way to parse JSON is simply the built in method to most modern browsers:

var obj = JSON.parse('{"name":"George Washington"}');

This is the implementation found from JSON.org and works wonderfully. The only problem is that older browsers such as IE 6, etc (more info form stack-overflow) don’t have this library built in and therefore you’d need to include the JSON.org library json2.js.

This is fine, but you are probably already using the jQuery library which includes this functionality. So instead of including even more libraries, let’s just go ahead and use the jQuery method which will work in all browsers.

var obj = jQuery.parseJSON('{"name":"George Washington"}');

From this function call we get back our JavaScript Object or null if the text was empty or an error occurred while parsing the JSON text. If you are getting null consistently, you may want to try validating the JSON you are attempting to parse using this tool.

A special thanks to Jared Klumpp for help in researching for this article!

JavaScript Click Handler Fix – Jumps to Top of Page

Posted on : 21-09-2010 | By : Sam Edwards | In : Developers, Tips

Tags: ,

2

You may experience the browser jumping to the “top of the page” when you click on link to “#”. This is because the browser is looking for the HTML element with the ID “#”, and when it cannot find it, takes you to the top of the page. To get around that, you can use this change, which replaces “#” with “javascript:void(0);”.

<a onclick="alert('hello')" href="#">click me (Before)</a>

click me (Before)

<a href="javascript:void(0);" onclick="alert('hello')">click me (After)</a>

click me (After)

After reading around on StackOverflow.com appears that you can return “false” from your event handler.

What is your method of handling this?

UPDATE 9/23/2010:
As “BU” suggested, it appears that returning false from an event handler is the proper way to do it.

Source: Corpocrat.com

Add Listeners to Existing HTML Elements with GWT

Posted on : 17-09-2010 | By : Sam Edwards | In : Developers, Tips

Tags: , , ,

0

GWT can be helpful, but if you are just adding a small amount of functionality to your website with GWT, you want to leave as much in tact of the original site as possible. In the example we’ll be using, we’ll want to pop up a modal contact form that the user can fill out and submit without which will not trigger any new page loads. This is where adding Event Listeners comes in. All GWT Widgets are made up of one or more HTML elements, all of which are part of the DOM that is built from the HTML and JavaScript. We have the ability, just like we can in JavaScript to add listeners to any one of those DOM elements. The following example puts a link on a “Contact Us” link, and trigger GWT code to handle the event.

Element contactUsElement = DOM.getElementById("contact-us");
DOM.sinkEvents(contactUsElement, com.google.gwt.user.client.Event.ONCLICK);
DOM.setEventListener(contactUsElement, new EventListener() {
    @Override
    public void onBrowserEvent(Event event) {
        // ONCLICK Event
        if (event.getTypeInt() == Event.ONCLICK) {
            Window.alert("Contact Us Clicked!");
	}
    }	
});

Hope this helps!

Sources:

Social Software Meetups

Posted on : 17-09-2010 | By : Sam Edwards | In : Developers, Events

Tags: ,

0

While searching around for this site in Google to get a better feeling for how this site is showing up in search engine results, I stumbled upon meetups! I’m a big fan of meetups because we are all “social” developers anyways, right? Getting together with the community to talk about development is a lot of fun, and a great place to learn. I discovered that there is a group type all around the world called the Social Software Meetup on Meetup.com. I am joining the Washington D.C. area meetup, but unfortunately am not in the area very often. Hopefully things will work out so I can attend though!
Check out this meetup and other meetups on Meetup.com and start learning.