tomhoppe.com

Archive for the ‘Javascript’ Category

Cross Platform Mobile Web Development

Tuesday, January 4th, 2011

Woah, long time no post! I guess I’ve gotten out of taking pictures recently, with mountain biking/training taking up most of my free time. Between that, twitter, and doing less actual development at work, I haven’t had much to post.

Recently though, I’ve been working on “mobilizing” a website, and man is it more of a pain then I thought it would be. The HTML and CSS is super easy, as you’d think. CSS3 and HTML5 is wonderful, and the site is devoid of image drop shadows, image rounded corners, etc etc.

The JS on the other hand, is fragmented. Especially dealing with things like slideshows, transitions, animations, and etc. It seems that even though multiple platform are using “WebKit”, that means something totally different for an iPhone then is does for an Android phone, or even Palm.

Then we get into QA. Oof. Not only does a Droid X with Android 2.2 act differently with touch events then an iPhone, but it’s different then an HTC Incredible or a MyTouch 3G running Android 2.1. What a pain. No wonder sites like mobile yahoo have moved to progressive enhancement of sites and provide a “mobile friendly kind of” site on Android, but give you the full fancy swipey site on the iPhone.

I’ll put a couple of posts out on the slideshow and swiping code that I ended up using, as it ended up being pretty clean and works well across multiple platforms, including degrading for non-touch browsers.

Goo.gl URL shortener bookmarklet

Tuesday, January 26th, 2010

I know there are already a bunch of tools for doing this same thing, but I’m a big google fanboi. The nice thing about goo.gl is they have an API which makes accessing the shortener easy. Matthew Flashen already wrote some JS for a bookmarklet for this. You just save that into your bookmarks, I saved it to my Delicious bookmarks, and you can call it on whatever URL. It alerts you the shortened URL and just copy and paste from there. Sweet!

Quick email validation regex

Monday, December 7th, 2009

So I don’t have to google for this one again, since I won’t remember it. Here is a quick regex to verify that your email address is valid. Should take care of most email addresses.

var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i;

Quick and easy mouse over delay with jQuery

Wednesday, April 22nd, 2009

On one of my side projects I wanted to do a hover over fade on and off menu, but wanted to do the menu items with text instead of images. After thinking about how to get the effect, I settled on the below solution. I also wanted a slight delay on the mouse over. That way, if someone just went through the links instead of on them, they wouldn’t get the fade.

HTML looks like this. The only drawback is for accessibility, that they get the link text twice. Not that big of a deal IMHO.

<ul>
    <li><a href="sports.php"><span class="off">Sports</span><span class="on">Sports</span></a></li>
    <li><a href="concerts.php"><span class="off">Concerts</span><span class="on">Concerts</span></a></li>
    <li class="last"><a href="theater.php"><span class="off">Theater</span><span class="on">Theater</span></a></li>
</ul>

Then the JS comes in and does a nice smooth fade on the “off” class. This allows you to get a fade rather then an instant “on” like you get when you just change classes and colors on the a element. I do a jQuery hover.

When it does the “mouseover”, it sets a class on the main list item its hovering on, that way the global scope of setTimeout will allow you to target that particular element to fade its child. The 100 ms delay is enough to get rid of most of the inadvertent hovers. If using this particular piece of code for something else, you can make that delay as long as you want for a delayed hover. Then the “off” state fades out.

The “mouseout” from the hover then clears the timeout and fades the “off” state back in and removes the hovering class as you don’t need it anymore.

$('#header li').hover(
function() { $(this).addClass('hover'); hover_intent = setTimeout( function(){ $('#header .hover').find('.off').fadeOut("fast"); } , 100 ); },
function() { try{clearTimeout(hover_intent);}catch(e){}; $('#header .hover').find('.off').fadeIn("fast"); $(this).removeClass('hover'); }); 

Cross Sub Domain Ajax and JSON

Friday, March 27th, 2009

I got a couple comments and emails about my post on cross sub domain JS and AJAX and using an iframe to make that happen. As I was making an example of it so the post made more sense, I thought about the cross sub domain issues we were having at work, and the “duh” solution to the issue of passing data between sub domains. Use JSONP instead of XML.

Its absurdly easy with jQuery and there are no iframes or anything else to mess around with. Just setup your js as a function call with a JSON object inside of it with your data.

myData({ "title":"test"})

Then on your page where you want the data, after adding jQuery just run the following

<script type="text/javascript">
function myData(theData) {
	alert(theData.title);
}
$.getJSON("http://test.tomhoppe.com/test.js?callback=?");
</script>

This will grab the test.js file containing the function callback and execute that function on your page. At that point you can do whatever you please with your data. Only do this with trusted sources of course, as the JS that you pull in gets immediately executed on your own page. This also has the negatives of not having status or error states. It just either works or doesn’t. But if you are worried about that, setup a server side proxy for your AJAX.