You are here

javascript

Javascript Message Window

I'm working on a popup message window for my uncle's game Dark Expanse. The idea is that clicking a star on the star map will pop up a small "e-mail" window displaying messages related to that star. The code is a work in progress, but I just hit a milestone and wanted to upload it quick.

At this point, the data is loaded using a $.getJSON call, displayed in the message selector, and clicking a message loads it into the display area. The last few hours have been spent integrating a scrollbar solution so that the scrollbars are always a fixed width, instead of dealing with browser and UI inconsistencies. I used Tiny Scrollbar created by Maarten Baijs because it was simple, straightforward, and very lightweight. I've also handled up and down arrow keystrokes, and set it so that the scrollbar updates when the newly selected message is outside of the viewport. I'm not sure if anyone else will find this code useful, and I'm not even sure what to call it, but perhaps someone else needs something similar.

Here it is

Format price output with Javascript

What follows is a script to format price output in Javascript. The function takes as its input a single number, forces exactly two decimal points, then adds commas for the the thousands, millions, etc. Finally, before returning the output it adds the dollar symbol and a minus sign if applicable.

Depending on where you are using this script, you may not be able to guarantee that only numbers are being passed to the code. For example, you may need to parse a string on the page to get a number before doing math (to apply a discount, calculate a price difference, etc). The first function will strip out non-numeric characters, then convert the number to a price with two or fewer decimal points. If I remember correctly, the multiplication and division by 100 not only serves to round to 2 decimal points, but also avoids a floating number problem (but I can't remember exactly, or I'd test it…).

// Take a string (possibly containing currency symbols and/or commas), // and strip out the extra characters, returning an actual number. function parsePrice(str) { return Math.round(Number(str.replace(/[^-\d\.]/g, "")) * 100) / 100; } // Take a number, and add all the necessary prettifying characters that // make a number look like a price (currency symbol, minus sign, commas). function formatPrice(price) { // Run toFixed to force exactly 2 decimal points // Note: This will round the thousandths up if applicable. For example, 3.145 // will be rounded to 3.15, but 3.144 will become 3.14 var value = Math.abs(price).toFixed(2), commaRe = /([^,$])(\d{3})\b/; // Add commas in convincing locations while (commaRe.test(value)) { value = value.replace(commaRe, "$1,$2") } // Add the appropriate currency symbol and minus sign if applicable return (price < 0 ? "-$" : "$") + value; }

So for example, say you want to apply a discount of 20% to a product page dynamically (not necessarily a good idea, but it's late and good examples aren't coming to me easily atm). The page lists the product price as "$8.99" in a DIV with the class="price". Using jQuery to get the DIV, you could write:

// Get the price from the page, and parse it to a number var listedPrice = parsePrice($(".price").text()); // Do our multiplication by .8 to get a 20% discount, then format that price and // and display it in the ".discountedPrice" element $(".discountedPrice").text(formatPrice(listedPrice * .8));

… or, you can do the entire bit on one line, if readability isn't an issue :-p

jQuery :checkbox replacement

I had to set up a checkbox replacement for the site http://www.reallysimplesurvey.com/sign-up, so I first went looking for a good replacement. I found one that I'd used before, but it required jQuery UI. Since I hate adding jQuery UI for "simple" tasks, and since trying to get jQuery UI working sanely in Concrete5 can be a bit of a trick, I decided to write my own (people who know me know it doesn't take much to convince me to rewrite Javascript just for kicks and giggles…).

I had some trouble with race conditions in IE, and I didn't spend too much time troubleshooting why, so the code might be sloppier than usual. Also, the only comments in the code at the moment are IE gripes, so my apologies :-p The use of double-class selectors means IE6 won't show when the checkboxes have keyboard focus, but all else should work without problem.

Please take it for a test spin and let me know how it works. This is the first release, so YMMV and feedback is welcome.

Screenshot of checkbox demo

jQuery SELECT Dropdown

I'm toying with some code to reproduce a SELECT dropdown using jQuery and some CSS3. The default SELECT element is notoriously difficult to style, but also amazingly difficult to program in a manner that works properly across browsers. I've seen many different projects along the lines of what I've done here, but I finally found one that I liked :-p The original code is from the blog Janko At Warp Speed. I will continue to clean up this code, but for now it is provided without warranty or promise of any kind, so YMMV. For the record, have not even tested in IE yet. Click below for the demo page.

Update (2011-02-23)

I finally got around to adding OPTGROUP support for a project I'm doing, and I added a few examples showing how the code can be used for site navigation (UL-LI-A dropdown navigation, instead of using a SELECT with an "onchange" handler). My next set of goals is to add keypress support, figure out the best way to make sure the dropdowns always show the same value as the SELECT (even after a page refresh), and maybe add a few more display styles.

Update (2011-05-23)

I'm still doing work on this project (amazingly). I finally organized the CSS, moving it and the Javascript out of the HTML and into their own files. I also added a download archive and slapped a license at the top of each file (I've been using this code frequently and the license links back to this site). I plan on adding SELECT MULTIPLE and keystroke functionality for a project. I'm hoping ARIA support is not far behind.

Screenshot of the dropdown demo

CSS3 tabs for your webpage

I wanted a nice, almost 100% CSS styled layout for my Javascript tabs. So with the help of the CSS Gradient Generator I set about doing that. All the gradients and rounded edges are CSS3-based, with the exception of the gradients used by IE (PNGs generated by the aforementioned website). Please check out the demo, let me know what you think. I have a feeling some fixes will be necessary for IE6, but I'm getting there…

I am also struggling to add CSS3 drop shadows to the tabs. I'm not sure if there's a simple way to do it so the tabs don't overlap onto the area below the tab. If someone wants to take a swing at it, please let me know.

Screenshot of the tab demo

Preventing Javascript alerts

This is a quick blog post, but I thought it was worth sharing. There have been a few times where I haven't had control over all the code on a page, and something on the page insists on spitting out an alert. Often this happens at the worst possible times, like when I'm trying to load another page in an IFRAME. I work with a lot of different e-commerce platforms (the current system giving me grief is Network Solutions Commerce Space, formerly Monster Commerce), and each platform does things differently.

If I want to add an item to the shopping cart using an IFRAME, the CommerceSpace platform spits out a Javascript alert every time an item is added. That's a little ridiculous (not to mention so 2000 and late…). So here you go… drop this in the page as early as possible.

window.alert = function(str) { window.console && console.log("Alert: " + str); };

That's it. Now any unwanted alerts will be sent to the window.console instead. If you still want to be able to use window.alert later, make sure to save a reference to it first (see Prevent js alert() from pausing timers at Stack Overflow).

I just found out about window.stop() as well… I'm thinking this might be fun and useful as well. Preventing the little "page loading" jogger from running longer than necessary? Or as Jeff said…

(01:56:59 PM) Me: whoa! how have I never seen this before? http://www.java2s.com/Tutorial/JavaScript/0380__Window/windowstop.htm
(01:57:06 PM) Me: Floating Cart? I'm just saying...
(02:28:07 PM) Jeff: all this talk makes me hungry for ice cream

Subscribe to RSS - javascript