The Blog

WCH3. Works in IE5.0+

I have to stop giving definitive sentences. I just decided recently that I’m gonna develop two version of WCH2, and than Tim Connor spoils it all and hands the idea of short and sweet way of covering IE5.01, without too much code. Oh well. :)

So, now I have 3rd generation of WCH, which covers IE5.0 and later. Also, I took the opportunity to rewrite the script the same way as ADxMenu. WCH is now an object, and you simply call its methods.

Take a look at this extensive example, where WCH successfully works with:

  • drop-downs
  • Flash movies
  • Quicktime video

As a side note…did you notice the W3C Schools are recommending non-standard way of including Flash or other media objects? Shameful. :(

How to use it

You need to include the WCH.js in your page, and then simply call the methods. I have renamed the functions to be less confusing.

WCH.Apply(Layer, Container);
WCH.Discard(Layer, Container);

So, when you are showing your layer, just call Apply method. When you are removing your layer, simply call Discard method.

News is that both parameters can either be an id or a reference. WCH will internally fetch the reference if id is passed.

How it works

Here is the part for those who want to further expand it, if they need to.

WCH object is created during page load, and a set of properties and methods is defined.

var WCH = new function() {
	//	internal properties
	this._bIE50 = (document.all &&
			document.getElementById &&
			!window.opera &&
			navigator.userAgent.toLowerCase().indexOf("mac") == -1);
	this._bIE55 = false;
	this._bIE6 = false;
	this._oRule = null;
	this._bSetup = true;
	//	public methods
	this.Apply = function(vLayer, vContainer) {
		if (!this._bIE50) return;
	};
	this.Discard = function(vLayer, vContainer) {
		if (!this._bIE50) return;
	};
	//	internal methods
	this._Hider = function(vLayer, vContainer)
	this._GetObj = function(vObj)
}

Internal objects are denoted with leading underscore. The entry guardian, _bIE50 is set during page load. The long condition is there is to ensure that only IE5+ Windows will execute the code. Any browser that understand Javascript will pass-through all the code, but you see that all but IE5/Win is sent back. This way, you don’t need to do if (WCH) WCH.Apply(...) but simply call the method with WCH.Apply(...). I prefer clean code. :)

The other detections (IE55 and IE6) are done on the first call to Apply. This must be done after the page load, and this way I don’t have to use window.attachEvent.

if (this._bSetup) {
	this._bIE55 = this._bIE50 && typeof(document.body.contentEditable) != "undefined";
	this._bIE6 = this._bIE55 && typeof(document.compatMode) != "undefined";
	if (document.styleSheets.length == 0)
		document.createStyleSheet();
	this._bSetup = false;
}

Basic functionality, for IE5.5+ is the same as before, the only difference being that sizing and positioning of iframe is done in the _Hider “constructor”, where it logically belongs.

The fun part is with IE5.0.

Hide and show with dynamic style rules

As with WCH 2.5, the only thing that page coder must do is to attach WCHhider class to any element that must be hidden when layer pops up. Make sure that, in your own style sheets there is no such class.

When Apply is called for the first time, script will add simple rule that hides the element, and save the pointer to the rule as WCH internal property.

if (this._oRule)
	this._oRule.style.visibility = "hidden";
else {
	var oSheet = document.styleSheets[0];
	oSheet.addRule(".WCHhider", "visibility:hidden");
	this._oRule = oSheet.rules(oSheet.rules.length-1);
}

Rule is added to the first style sheet loaded1. If there are no style sheets, one is created during setup (see code above).

On any subsequent call, simply change the rule to either visibility:hidden (for Apply) or visibility:visible (for Discard). That is all. Simple and sweet.

1 : This could be problematic if you use style switcher, when you must add the rule to every style sheet.

Banca

Banca

Beautiful and functional currency converter, supports just about any currency in the world.

Go Couch to 5k

Go Couch to 5k

The most popular starter running program in beautiful feature-rich app (GPS tracking, charts, detailed history etc)

Quickie to do

Quickie to do

The fastest short-term task-list / check-list app on the App Store. Really.

Guerrilla Cardio

Guerrilla Cardio

The most challenging high-impulse interval training in the world.

Run Mate

Run Mate

A versatile running coach app, with unlimited number of running programs. Perfect for casual runners.

10 Comments

Feel free to chime in, looking forward to it. Leave a Comment

  1. Ron Derksen says:

    Hi Aleck,

    could you tell me if the hider only works for –objects and iframes, or if it also works for other elements, e.g. java applets?

    Thanks,
    Ron

  2. aleck says:

    Ron, I haven’t tested it with Java. I don’t have Java VM installed as I don’t like it.

    Examples for version 3 shows that WCH can handle Flash and Quicktime movies.

  3. Tim Connor says:

    Thanks for the credit, Aleck. Now I really need to get off my ass and finish my (standard-compliant) redesign. ;)

    Looks good — I like clean code too :). I like the idea of using underscores to distinguish “private” function — I’ll be borrowing that one. I’ve got a project that I need to make a first delivery on this week-end that is basically using pure css menus with just the WCH (and the whatever:hover) grafted on, so I’ll be doing some testing for you, also. ;)

    Later, I’ll be looking at adding this onto an existing menu on the project that got me started tweaking your code — the one where I have to deal with stuff over Flash in N6. It appears that Safari has a similar problem (the old iMac I eBayed for testing with finally arrived — so nice, even for an oooolllddd machine), with both Flash and Quicktime, so I’ll definitely be extending things — unless one of the other developers comes up with something simpler.

    For that project, I was thinking I was going to have to go back to my using a whole stylesheet thing, to give more flexibility on what to do in those cases, but I just thought of a better technique, that gives a nice mechanism for dealing with no plug-in substitution, at the same time. I’ll just wrap the Flash/whatever else in a div that has a background of the image substituiton that I was going to use directly on the object. Then when it goes hidden, walla, substitute image while menuing, instead of just losing the whole Flash, in a flash.

    It’s annoying how addRule doesn’t return you a usable index or reference isn’t it?

  4. aleck says:

    > It’s annoying how addRule doesn’t return you a usable
    > index or reference isn’t it?

    Very annoying. MSDN says it’s a reserved value. I wonder for what…

  5. Tim Connor says:

    Oh, and it turns out you can, essentially, get private/public in javascript : Private Members in Javascript

    With that in mind, a retooling using his suggestions for effecting private/public (could still had your underscores). At least one less comparison each time, around, and some other tweaks. It adds oSelector, which lets you easily set the selector for oRule (.WCHHider vs. select thing vs. some other combo).

    WCH3_tim.js

  6. Aleksandar says:

    Tim, thanks for the link, it sure reveals stuff about Javascript I was not aware of. I will certainly recode all my scripts in this manner and possibly ste…err, borrow some of your tweaks :).

  7. Nate says:

    Folks, I’m using some ToolTips code (an example and source code can be found here: http://www.griffininteractive.net/archives/web%20design/000124.html ) and I can’t seem to figure out how to get it to play nice with the WCH.Apply on drop-down boxes — the tool tips remain behind the drop-downs, regardless of what I do.

    It seems somehow problematic that the layer to be displayed lives inside an anchor tag, but I haven’t been able to get much further than that…

    Ideas?

  8. Aleksandar says:

    Nate, if I’m seeing correctly, you use span inside of a, and that span is the tooltip layer. You need to place an ID for that span and then pass the ID to WCH.Apply.
    Other option is to write a function of yours, fetch the tooltip object and pass the object itself to WCH.Apply.

    Second parameter for WCH.Apply (container) is the id or reference of the a element (with class=“info”).

  9. Stuart says:

    Has anyone used this successfully with sInman Flash Replacement? http://www.mikeindustries.com/sifr/

    This method applies a generated class and is therefore not present in the html. How would you use WCH in this case? I have tried amending the class in WCH.js without success.

    You can see what I have tried here:
    http://www.cofieldwines.com.au/AboutUs.jsp
    http://www.cofieldwines.com.au/Includes/Internal_Style.css
    http://www.cofieldwines.com.au/Includes/WCH.js
    Regards,
    Stuart

  10. Aleksandar says:

    Stuart, is this related to menu at the top. It seems to work OK in IE6. In general, WCH would be applied the same way as in any other case — submenu (oLayer) + its positined parent (oContainer).

Comments are now closed for this article.