There are some very basic and fundamental uses of these, which can be furthur extended to be used to complicated and more efficient, useful tasks. They take away the pain of hardcoding everything into your document. You can leave subtle, easy to enter hints for your script to look for, and once found, alter the line in some way or do something else. With Javascripts regexps as strong and useful as they are, there is no reason why people with enough coding knowledge shouldn't exploit them.
I employed something like this on my blog last night. A nagging but very visible problem that I have been having is differentiating between external and internal links in my posts. I always want my external links to open in a new window for the user, but I needed a way of telling them that the link would open a new window. Now, the fool's way would be to hard-code some kind of identifier inside the link tag to let the person know, but I decided a better way. Why not let a script run through my links, and alter them if they are marked as external links?
So I made up a simple but useful script! It checks the rel
attribute of all links, and whenever it finds a match for external
or ext
, it'll append an image to it, and make its target="_blank"
. Simple, unobtrusive, and saves tons of hardcoding time. Although a problem arises if you want certain links not to be affected, yet keep them external. But for that, you can always use a third value for rel
.
This is only one of the basic uses of scripts to do repititive tasks. Scripts are powerful things, and should be used whenever their need be sensed. Ofcourse, don't go overboard otherwise your page will have ten's of scripts running at page load and hence will slow down the loading times. That is not good! :) So find innovative ways of using this, you won't regret it!
[Info]: The auto external links script is something like Kirk's over at Phydeaux3. I'll admit the idea stemmed from there! :)