Paul's Dev Blog – iGonzo.net

Im not selfish. I just care about myself. A lot.

Archive for the ‘javascript’ tag

Javascript hates spaces

without comments

It’s the basics that someone often forgets. This is true in any profession once you progress to a certain level. The basics usually just come naturally, so people often don’t think that it could be something simple that is causing erros.

Javascript, for example, hates spaces. I knew this since I started programming Javascript. If you do not accomodate for them, it can and will lead to problems. Which is what I spent an hour trying to figure out last night.

See, the user can upload images into the gallery. The script replaces the background of a div with the image that was clicked. Pretty simple and clean, if I do say so myself. However, the user could upload images with just about any naming convention they wanted. This is what was causing the issues. The spaces in the names of the file that were uploaded. The script would come across this and crap out because I did not accomodate for spaces.

The solution was simple enough; replace the offending character with its HTML encoded equivalent, in this case using “%20”. After this was done server side, the script worked just like it should, and I commenced with beating myself about the head with my Javascript Bible book.

So the lesson of the day? If something isn’t working, and you know that it should be, go back to the basics. Are my quotes in the right place? Are there any offending characters that JS doesn’t like? Did I include the file with the correct path? These things will often be the cause of the issues and you will be greatly relieved that you spent the five minutes checking the basics (and often solving the issue), rather than wasting an hour ignoring the stuff you knew was right.

Cheers

Written by iGonzo

January 24th, 2011 at 10:39 am

The bane of JavaScript

without comments

So I just spent the last hour wondering why my GMaps implementation was not working on a particularly complex site. More to the point, I was wondering why it was working on the live site, but not on the testing site. That is, until I looked at the live site and saw it wasn’t working there either.

Uh oh.

So I started looking at the usual suspects when my JS isn’t working; forgot a semi-colon, misfiring functions, garden gnomes, etc. I then started looking at various doc-types, versions of jQuery and my GMaps API, even my content-type settings, and nothing was working.

Then it hits my like a lightning bolt: 90% of problems I deal with are from content. Specifically, content that has a lot of strange characters that HTML generally, and javascript in particular, doesn’t at all like. So I go through the dynamically rendered markers for my google maps’ points and find, in the 50 or so points I am rendering, there is one name that has an apostrophe in it.

Grrrrr.

A quick replace function in the code and, low and behold, shit starts working again. To make sure it doesn’t happen again I am now running the content through our nifty illegal characters replace function because, in the future, someone may enter a name or something that has a tilde character in it or something else equally strange.

The point is, boys and girls and others, that if you are dealing with javascript, especially if it is very large and complex javascript, be sure that there are no floating apostrophes out there.

On a side note, I am not against apostrophes  in general. I just believe that languages can do without them. I mean seriously; when was the last time you texted a properly formatted message to someone?

Thats what I thought.

K Thx Bia

L8r

Written by iGonzo

October 26th, 2010 at 1:06 am