easytoggle and debugging in Safari
I’ve been working on a new inobtrusive DHTML effect: easytoggle, which is an inobtrusive implementation of the common effect where links or tabs can be clicked to reveal part of a page while hiding the other parts. It’s similar in some ways to the Multi part forms with Javascript technique.
The idea is pretty simple—all you need are a bunch of links and a bunch of elements that should be toggled by those links. When adding special behaviour to links it is always a good idea to ensure that they still link to something sensible, so that in user agents without Javascript support they still do something useful. In this case, it makes sense for them to act as anchor links that point to the elements with which they are associated. With easytoggle, all you need to do is define the links, point them at an element with an ID and assign them the class ’toggle’. The script does the rest of the work. For example, for a simple set with only two panels the markup would look something like this:
<ul>
<li><a href="#p1" class="toggle">Panel 1</a></li>
<li><a href="#p2" class="toggle">Panel 2</a></li>
</ul>
<p id="p1">This is panel 1</p>
<p id="p2">This is panel 2</p>
That’s all it takes—the demo has a very slightly more complicated example.
I thought it was finished, until I tested it in Apple’s Safari browser. In Safari, the worst possible thing happens. The initialisation code which hides all of the panels after the first one works fine. Unfortunately, the code that causes the links to change which panel is visible fails silently, leaving only the first panel accessible to users with that browser. That’s a big problem.
It was at this point that I discovered that Safari’s support for debugging Javascript sucks rocks. Firstly, the browser gives no indication that a bug has been encountered. I’m sure this is a deliberate usability decision, but it also means users who encounter a bug won’t even know that there is a problem with the site. A quick Google led me to this page, which told me how to enable Javascript error reporting:
- In a command line shell, execute
defaults write com.apple.Safari IncludeDebugMenu 1(apparently Safari Enhancer lets you do this from a GUI). - Reload Safari and check the “Log Javascript Exceptions” option in the newly enabled Debug menu.
- Start Console.app, which lives in /Application/Utilities. Note that this is not the same thing as the command line console. This took me a few moments to figure out. Macs remain a strange new realm of discovery.
- Javascript exceptions will now appear in the Console.app window.
Excellent—just what I needed to know. The entire error message I got when I clicked a link? (event handler):Undefined value. Gee, thanks a lot Safari. If anyone has any ideas how I can fix the script in Safari (or at the very least prevent it from being unusable) please leave me a note.
Update: Thanks to a tip from David Lindquist, the updated version of the script now works in Safari. It’s a little bit uglier though.
David Lindquist - 6th November 2003 01:58 - #
preacher - 6th November 2003 02:51 - #
Simon Willison - 6th November 2003 03:22 - #
sil - 6th November 2003 06:44 - #
Simon, the CLI is in the Terminal, not in the Console ;-) And that distinction is not particular to Mac OS X.
padawan - 6th November 2003 08:23 - #
ppk - 6th November 2003 13:41 - #
ppk - 6th November 2003 13:42 - #
P01 - 6th November 2003 19:03 - #
First: Not all browser support targets that are IDs, so you should probably do a named anchor in there, too.
Second: They're planning on removing the javascript console from Firebird and turning it into an extension. I think there will be a statusbar icon to indicate a problem, but no real information with the default configuration.
alanjstr - 6th November 2003 22:37 - #
Simon Willison - 7th November 2003 00:25 - #
oli - 9th November 2003 06:43 - #
simon - 10th November 2003 16:46 - #
VQC Designs, LLC - 19th December 2003 02:03 - #
TvA - 16th April 2004 15:56 - #
waf - 27th April 2004 10:22 - #
A nice script but I have one comment. Wouldn't it be nice, if one made a script, where you wouldn't need to make internal links, but instead just took an e.g. <h3> as the name of the tab:
That would put the content and the name of the tab closer together!
Lars Olesen - 28th April 2004 14:34 - #
Great script, Simon.
I was wondering if there's a way to have more than one instance on a page. I'd like to use it in 2 places, but am not proficient enough to re-write the code.
John Fujiwara - 13th May 2004 17:14 - #
Simon,
this is a great script, but i have 2 issues and a nice-to-have i hope you can address in a subsequent release.
anyway, this is not to take away from your excellent script. keep up the good work.
Cris - 10th September 2004 03:37 - #
Antioche - 3rd October 2004 19:07 - #
For those whom want this script to work with images instead of text.
Mary - 15th January 2005 05:14 - #
sawa - 13th February 2005 09:27 - #
Brett - 11th March 2005 14:18 - #
The javascript console can be found in the debug menu, it is the 14th option, directly under "Log JavaScript Exceptions".
Also I have to take a minute here while on my soapbox to plug MochiKit (www.mochikit.com), a nice set of JavaScript libraries that make writing it far less painful and somewhat more pythonic. If you write JavaScript, it will make your life better (but your pageloads slower :-( )
James Kafader - 9th March 2006 21:45 - #
You have coded a great piece of script. It was very easy to modify it for CSS usage.
I send you my modifications, which ables color coding to maintain some "you are here" kind information, that was requested by a commenter; Cris - 10th September 2004.( I hope it is not so late!)
Briefly, I added id's to li's, as li+href.value(after hash); like "lione", "litwo".... assigned a CSS class to li's, named menulist. Added a few styling for menulist and menulistselected in HTML document. And some code to the .js file to assign these class names occasionally.
I think you don't mind if I use it on some site.
Many thanks.
ilker - 29th March 2006 18:06 - #
Great stuff, but I like this one better:
http://www.bobbyvandersluis.com/articles/unobtrusi veshowhide.php
Uses images and/or text and seems leaner and meaner to me.
tatlar - 3rd May 2006 20:11 - #