Archive for the 'JavaScript/DOM' Category

Introducing onViewable

Here’s my second resuable script to hopefully find a home in the wild wild web… onViewable. Instead of duplicating all the details of the script here, please see it on it’s own page.

The jist of the script is that it’s a function that allows a snippet of code to be fired once an element is viewable on the screen.

We have a few longish type pages where I work and I wanted to use the Fade Anything Technique to do what it does best, and hilight a few elements on the page. Well a few of those elements were not viewable on the screen above the fold so I had to come up with something to make them fire when they were viewable. So long story short…I slapped together a few miscelaneous things I had laying around and onViewable was born.

Please find the details of the hows and why’s on the onViewable page. Any updates will be posted there, and I will mention them in the blog so you can be aware of them.

I look forward to any feedback.

I must give credit here (in addition to on the script’s page) to the great PPK (Peter-Paul Koch) of QuirksMode.org. Without his invaluable resource and a few functions I borrowed, this wouldn’t be here today. Thank you PPK for your great contribution to the community.

FastrFriends Bookmarklet Version .2

When playing the fun new game Fastr with a few friends it was difficult to see where they were in the rankings with it being so popular (100+ people in the game at a time). So I wanted a way to easily see where they were.

So, here’s a super quick and dirty bookmarklet for tracking friends while playing. This is like 30 mins of work and far from complete and perfect I’m sure. It’s also based on the current DOM of the page, so if that changes, this breaks. But since it’s centrally hosted I can compensate if that happens and the next time you use it after I fix it, it will work.

To use it, drag the following link to your toolbar, and then click that button after starting a game of Fastr. FastrFriends. After it’s loaded up, simply click a name to follow it, and click it again to stop following it (if you got the wrong one or whatever).

Potential upgrades I’m pondering:

  • Option to show/hide all users that aren’t your friends
  • Different colors depending on how high on the list they are
  • Track their score and then show what they got on the last guess when it changes.

I know it works in FF 1.5 and IE 6. I’ve been told it doesn’t work in Safari but I don’t have easy access to that, so if anyone can tell me what to fix I will give it a try. Otherwise I will try and get a hold of one at work on Monday.

Report any bugs here… and enjoy!

VisitorMap plugin for ShortStat

VisitorMapThis is just a quick plugin for ShortStat that will put your visitors on a map using the Google Maps API. I wanted to get a quick visualization of where visitors were coming from, without having to add more scripts to my site from some of the other services that have come up lately (gVisit, MapStats).

VisitorMap uses the existing information that is already saved by ShortStat. It is meant to be a plug-n-play addition that doesn’t require any database or other changes. The only required change is in the file itself to set the Google API Key (free) for the folder you are installing it in to.

This is a quick and dirty release. Some of the data parsing I am doing could probably be done cleaner, but it works for being a late night “for fun” project… at least for now.
(Read the article)

Introducing onDOMload

Today I release my first reuseable script into the wild. Hopefully it will be found useful and can help other projects move along as well.

Thanks to the new pages function of WordPress 1.5 this will live at it’s own location, so there can be a single point of reference for any changes and such. But I will post any updates made to it here in the blog so that anyone who cares to, will know about it.

I will put the full text from the page here for the initial entry (and for searchability since pages aren’t searchable yet through WordPress), but all future changes will be found on the page itself.

Comments, feedback and suggestions are greatly appreciated.

Information

Script Name: onDOMload
Current version: 1.1
Last Updated: 03.31.2005
Download: onDOMload

Purpose

To allow JavaScript to run on a page after the head, but before onload and before an “at the end of the page” (in-content) JS script fires.

What is it?

This script starts looping trying to run it’s code until the page is done loading. The code is continually checking for certain DOM elements to load from when the functions are called, until an onload call stops it. Using this method, the code you want to run will happen as soon as the loop runs again after a given DOM element with a certain tag/id/class is loaded, not onload or when another in-content JS call is made at the bottom of the page.

How does it work?

You provide two things.

1 - What you want the code to be run on - You can pass an ID (#theID), a class name (div.class), an element name (td) or several, but not all, combinations of the above (div#theID h4).

2 - What code you want to be run - You can pass it any code that you want, as it does an eval on it when it’s time to run it. I know that’s not super efficient, but if anyone can think of something better, please let me know. Since the code can be run multiple times, it provides a keyword of “theTarget” that references the current element being manipulated.

Every X milliseconds the script tries to run your code again, looking for the DOM elements you provided. When it finds one it runs the code on it, and then keeps track of how many it’s done. When the loop happens again it compares how many it did last time, with how many there are now. If there are more then last time, it starts with the next newest one and runs through the rest of them. If it doesn’t find anything new, it keeps looping and checking for new ones each time.

An onload is run that says the page is done loading. It then stops the loops so that they do not continue to run as you are just looking at the page, and does one last run just to make sure nothing showed up between the last loop and the onload.

Why use it?

As mentioned above, until now there have been 2 ways to fire code later on down the page. 1 - onload, which waits until every element (img,script,css,etc) has loaded on a page. If an advertisement, or stat tracking pixel, or whatever hangs for a while… your script can’t fire yet. 2 - “in-content” reference. This is a tad better in that you can call it at the bottom of the page, and it runs as soon as it is read by the browser. This again can potentially be slow if the page is very long, or if something server side hangs the page before it gets to it, but is definitely faster then the onload.

onDOMload allows code to run much sooner during the pageload. If the code you run makes changes to the looks of the page, then this will allow it to happen much faster then the other versions will. It also allows you to keep all your code in one location… at the top. Instead of needing part of your code at the bottom.

History

I had thought of this concept dozens of times, but finally started playing with it after the Man in blue posted about it and gave an example. The need for this became pressing for me when working with a version of IFR which had to wait till the end of the page to do the replacement. We would always see the headlines loading after the page was mostly rendered, and it didn’t look all that great.

I have improved on The Man in Blue’s version (I hope) by adding the ability to run code on elements with a specific class, ID or just on all instances of a given element. An earlier version of the script just had ID and tags available (the unreleased 1.0 version), but I leveraged the code from Alessandro Fulciniti’s Nifty Corners (which are pretty dang nifty, btw) to improve the ability to direct the code. You can also reference the current element by using “theTarg” in the passed code… kind of like a “this” call.

See it in action

Example 1 - Several different types of schedulers on the same page.
Example 2 - Example of how an in-content or onload call can delay your intended actions.

Compatibility

Works fine in FireFox 1.0.1, Win IE 6.0 SP2, Safari 1.0.3 (if anyone has a newer version they can confirm in, please let me know)

Partially works in Mac IE 5.22. I may get this working in the future, but for now it’s not a priority.

Changelog

03.31.2005 - 1.1 - First public release. Fairly decent overhaul from the unreleased 1.0 version. Added ability to go off of classes and IDs. Added final check after onload fires.

1.0 - Never actually released

IE6 png Alpha Transparency Script

Eric Meyer just posted an updateto his s5 script in which he has added support for alpha transparency for IE6. The fix comes thanks to Erik Arvidsson’s pngbehavior.htc. Very cool… sucks to have to hack around IE yet again but what are you going to do?

His one lament about this fix:

The one sort-of drawback to using this approach is that it seems to require that the call to pngbehavior.htc sits in an embedded style sheet, or else nothing happens. This may very well have to do with the way the JavaScript monkeys around with external style sheets during startup. If any of you IE/Win JS gurus can figure out a way to get the behavior to fire without having to embed it into the presentation, that would be stellar. If not, it’ll just be documented as a “leave this in if you’re using alpha PNGs; otherwise you can take it out” thing.

Now I don’t pretend to be an “IE/Win JS guru” but decided to play around anyway. Using the simpler example at the top of Erik’s page I saw what the “bare necessities” were. Now that I go back and look at his .htc behavior closer, I see that I am doing some of what he was doing there… but just in the JS.

What I came up with is a quick little JS script that cycles through all the images on the page, if an image is a png (detected by the extension) then it applies the appropriate filter and replaces the image with a clear .gif file. This eliminates the need to have any image specific references in the CSS or JS, and the need for the HTC file altogether.

See a demo of the script in action.

I’d get all fancy and separate out the script and stuff for you to easily peruse, but I probably need to get back to work. So just view source and poke around.

Hopefully it is found useful, and will be a help to Eric’s issue. Thanks to Erik for the initial script and research. Feedback is welcomed as I’m always willing to learn.

Next Page »