Amass

slideLeft and slideRight in jQuery

November 28th, 2008 Luke

If you’ve ever wanted to slideLeft or slideRight in jQuery, you may have noticed that you…can’t. You can slideUp and slideDown, but not slideLeft or slideRight. Why is this? I don’t know. I do know that getting those effects is a single line, though:

obj.animate({width:'show'});
obj.animate({width:'hide'});
obj.animate({width:'toggle'});

Simple, and easy to use. But why isn’t it in the core?

Checking if a date is between two other dates in Javascript

December 21st, 2007 Luke

This is a handy little function I whipped up that will allow you to pass in three dates, and check if one date is between two others.

function dateWithin(beginDate,endDate,checkDate) { var b,e,c; b = Date.parse(beginDate); e = Date.parse(endDate); c = Date.parse(checkDate); if((c <= e && c >= b)) { return true; } return false; }

Enjoy!

Javascript has no ignoreWhite property

June 8th, 2007 Luke

This is a problem that was driving me absolutely batty a little while ago.

In ActionScript, there’s an ignoreWhite property that you can set to true for any XML objects that you’re using. This makes it so that this:

<menu> <item> test </item> <item> testy </item> <item> sixty-eight </item> </menu>

Will properly end up as “test”, “testy”, and “sixty-eight” if you were to create an array of items.

However.

Under Javascript, there is no ignoreWhite property. You need to make sure that whatever is outputting your XML is also removing the whitespace from it, because otherwise you’ll have issues. Your XML should end up like this, if you want the same results:<menu><item>test</item><item>testy</item><item>sixty-eight</item></menu> Otherwise, you’ll end up with nodes in places you didn’t expect them to be, and values like “&tab;test” instead of the “test” you were expecting.

Javascripted Useable Forms

May 28th, 2007 Luke

With the rising popularity of Ajax, more and more people are starting to build webapps that rely heavily on Javascript. In a lot of cases, the HTML form element is also being used (or at least seems to be), in order to show a user where they are supposed to input data. It is not uncommon to see Ajax-enabled register/login pages that look exactly like regular register/login pages: Parakeet’s “Register” page is a good example of this.

However, a problem with this style is that, at least in some cases, the new javascript controls mean that the form no longer works as the user expects. This is bad for usability, and I’ve managed to figure out that the whole problem can be resolved (and unobtrusively, no less) by making use of the onsubmit parameter of the form tag.

If a user submits a form and there is a value for its onsubmit parameter, the web browser knows to call that function when the form is submitted. It then transmits the form data to the URL specified by the form’s action attribute. If there is no action specified, it sends the data to the page that it is currently on, subsequently refreshing the page.

When we write return false; inside an HTML attribute, we instruct the browser to run the code before it, and then return false. The action of returning false means that the browser will stop processing; whatever actions follow after the return false; statement will be ignored. The great part of this is that in a browser that doesn’t support Javascript or has it disabled, the parameter with the return false; statement inside of it will just be silently ignored. This allows us to make websites that work flawlessly for users with Javascript either enabled or disabled, with a minimal amount of extra effort.

By combining the onsubmit attribute of a form and the return false; statement of Javascript, we can make our forms react the way that a user would expect them to, whether Javascript is enabled or disabled. Take a look:

<script> var d = document; // shorthand variable function handleFormSubmission() { var inputValue = d.getElementsByTagName('input')[0].value; alert('The Value of the input is: ' + inputValue); } </script> <form onsubmit='handleFormSubmission(); return false;' action='http://girasquid.com/sample_code/jsdisabled.shtml'> <input type='text' /> <input type='submit' /> </form>

If you want to see how it turns out in an actual page, take a look at the Hardcoded onsubmit sample code I’ve put up.

This method involves painstakingly hard-coding the onsubmit attribute for each form element, though. Wouldn’t it be easier to just use a window.onload event?

The answer is yes. Here’s the new code:

<script> var d = document; // shorthand variable function handleFormSubmission() { var inputValue = d.getElementsByTagName('input')[0].value; alert('The Value of the input is: ' + inputValue); } // This anonymous function will attach our 'onsubmit' action for us, so that we don't add anything to our HTML markup. window.onload = function() { d.getElementsByTagName('form')[0].setAttribute('onsubmit','handleFormSubmission();return false;'); } </script> <form action='http://girasquid.com/sample_code/jsdisabled.shtml'> <input type='text' /> <input type='submit' /> </form>

To see it in action, check out my window.onload onsubmit sample code.

Rewriting my Javascript

February 6th, 2007 Luke

A friend and I are busily sketching out a project idea that, among other things, involves a countdown. For a while, I couldn’t figure out how to do it. But after giving it a little bit more thought, I realized that all we’d need is a little bit of javascript and an initializing function that would begin the countdown, along with refreshing the page when the countdown was finished(most of the real ‘actions’ per se are run by server-side code, not client-side).

This was all well and good, and last night I happily sat down and hacked out a solution. You can see it in the portfolio section of girasquid.com. And it works, and it does what it needs to.

But at the same time, it’s not as … robust as I’d like it to be. It doesn’t do all the cool things. It won’t let me put more than one countdown on a page without hard-coding that. It won’t let me spawn off lots of countdowns at once with all different times, and then independently start and stop them.

For some of my projects, I encounter a problem like this and just go “oh. Well. I’ll do that later.”, and put it on my “torewrite” list(which is growing rather large, unfortunately). But this is a piece of code that’s generally useful, and I think that if I could develop it further so that it was perhaps object-oriented and more flexible, other people would use it. And that would be sweet. Therefore, I’m in the process of redesigning it. This is what I’ve sketched out.

We would have a Countdown object. It would have different member variables, that would make it easy for a separate script to interact with it’s parts. These are the variables and their breakdowns:

  • itsHours
  • - the number of hours remaining in the countdown.
  • itsMinutes
  • - the number of minutes remaining in the countdown.
  • itsSeconds
  • - the number of seconds remaining in the countdown.
  • itsCounting(boolean)
  • - whether or not the counter is actually moving or not
  • itsRemainingSeconds
  • - the number of seconds total(hours + minutes + seconds)
  • itsRedirect
  • - where to redirect the user to, once our countdown has finished(refresh/no action if empty)
  • itsInterval
  • - the interval we were using to continuously update our timer.

And then we would have different methods that we’d be able to call to actually do our counting. Here’s a sketch so far:

  • initialize(seconds,redirect)
  • - This creates a Countdown object for us, and sets the two member variables we’re supposed to be able to set.
  • start()
  • - this would actually start the countdown, along with setting itsCounting to true.
  • stop()
  • - this would stop the countdown at the second it was currently at, and set itsCounting to false.

There would also be multiple utility functions for things like making sure the hours/minutes display correctly and such. I’m planning on introducing one function that lets a user pass in a string with certain placeholders, and have the object return the string, with the placeholders filled in with their respective values. That would be cool.

I’m going to get to work on this.