SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    div zindex swapping in different browsers

    Hi

    putting together a div zindex swapping function which I have working like so...

    Code:
    function swaplayer(thelayer, otherlayer)
    {
    
    	if (navigator.appName == "Microsoft Internet Explorer")
    	{
    		/* change their zIndex */
    		myid(thelayer).zIndex = 2;
    		myid(otherlayer).zIndex = 1;
    	}
    	else
    	{
    		/* get the elements ids */
    		thelayer = document.getElementById(thelayer);
    		otherlayer = document.getElementById(otherlayer);
    
    		/* if they are not undefined */
    		if ((typeof thelayer != "undefined") && (typeof otherlayer != "undefined"))
    		{
    			/* change their zIndex */
    			thelayer.style.zIndex = 2;
    			otherlayer.style.zIndex = 1;
    		}
    	}
    
    }

    and is called in the div like so

    Code:
    <div id="twoouter" onclick="swaplayer('twoouter', 'oneouter'); return true;">my div</div>
    this used to be a very simple script which i updated because what worked in ie (myid(thelayer).zIndex = 2 did'nt in Safari. At the moment I am only checking ie and Safari is there anything else I should check which nether of the above will work for.

    thanks for any help in advance.

    a+
    gar

  2. #2
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The else part of your code should work in every browser that's relevant at the moment. This includes IE so you could drop the if (navigator.appName) check which is considered bad style these days anyway.

    However, your check for non-existing id's is broken. document.getElementById is defined to return null if it can't find the id and typeof null is not 'undefined'.

  3. #3
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Saturn
    The else part of your code should work in every browser that's relevant at the moment. This includes IE so you could drop the if (navigator.appName) check which is considered bad style these days anyway.
    older versions of ie (5.2 on a mac - there is no ie 6 for the mac) don't work with just the else clause.

    Quote Originally Posted by Saturn
    However, your check for non-existing id's is broken. document.getElementById is defined to return null if it can't find the id and typeof null is not 'undefined'.
    not too sure what you mean, could you elaborate?

    a+
    gar

  4. #4
    SitePoint Addict
    Join Date
    May 2004
    Location
    Europe
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    older versions of ie (5.2 on a mac - there is no ie 6 for the mac) don't work with just the else clause.
    Really? Strange, both getElementById and element.style should be supported by IE 5.2.
    not too sure what you mean, could you elaborate?
    This line:
    if ((typeof thelayer != "undefined") && (typeof otherlayer != "undefined"))
    won't work. Try to pass some non-existing id to your swaplayer function and you will get a JavaScript error. Use either

    if (thelayer != null && otherlayer != null)

    or, more JS-like,

    if (thelayer && otherlayer)

  5. #5
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Saturn
    if ((typeof thelayer != "undefined") && (typeof otherlayer != "undefined"))
    won't work. Try to pass some non-existing id to your swaplayer function and you will get a JavaScript error. Use either

    if (thelayer != null && otherlayer != null)

    or, more JS-like,

    if (thelayer && otherlayer)
    ah ok noted. thanks for your help.

    a+
    gar


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •