Paul's Dev Blog –

Is that you John Wayne? Is this me?

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.


Written by iGonzo

January 24th, 2011 at 10:39 am

Leave a Reply