SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to make an if stament that will do a thing if the browser is Internet Explorer. Else if the browser is Netscape will execute other code. How can I make that?

    if (Internet Explorer)
    {execute this code}

    else if (netscape)
    {execute this other code}

    ?
    Thanks


    PS-also, is there a tutorial or reference that sais what commands are supported by each kind of browser?(most important for me at the moment is knowing wich commands are supported by browsers to show and hide layers..)
    Last edited by tabsquare; Apr 26, 2001 at 06:44.

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2001
    Location
    Toronto, Ontario, Canada, Earth
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello,

    try this:

    Code:
    var browserName=navigator.appName; 
    if (browserName=="Netscape")
    {
    // exectute this code 
    }
    elsif (browserName=="Microsoft Internet Explorer")
    {
    // exectute this code 
    }
    this will detect which browser is being used. however, i strongly recommend that instead of doing this you should, whenever possible, determine if the browser supports a certain feature. here's an example for a user to automatically bookmark a page in internet explorer:

    Code:
    if(window.external) {
    window.external.AddFavorite('http://www.yoursite.com/','Your Site')
    }
    this will insure that all browsers which support that function will have it done, and it will be ignored by browsers which don't support it. also, since you can't include all the minor browsers such as opera or webtv in the browser detection, this makes sure everything works the way you want.

    Jason Weinstein
    webmaster@MaximumEdge.com
    http://www.MaximumEdge.com/

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks max
    yes, that's better I think too.

    Actually If I had a lot of pages I wouldn't check for minor browser, but since I have only a couple pages to test, I think I will make it the more browser compliant possible


    the PS question? ^

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    here's another question. Hopefully the last one

    I use the below code to show a layer in IE when a link is passed

    onmouseover="document.alllayername].style.visibility='visible'"[

    and this one is the code to hide it

    onMouseOut="document.all['second'].style.visibility='hidden'"

    these two doesn't work on netscape....wich is a code that works for netscape that has the same output?

    Thanks
    Last edited by tabsquare; Apr 26, 2001 at 09:32.

  5. #5
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    To hide show layers in Netscape 4, you have to use:

    Code:
    document.layers['layer_name'].visibility = "hide";
    document.layers['layer_name'].visibility = "show";
    For Netscape 6 you have you use

    Code:
    document.getElementById("layer_name").style.visibility = "visible";
    document.getElementById("layer_name").style.visibility = "hidden";
    So for it to work on all browsers, try this:

    Code:
    <html>
    <head>
    <script>
    function show(){
      if(document.getElementById) {document.getElementById("layer_name").style.visibility = "visible";}
      else if(document.layers) {document.layers["layer_name"].visibility = "show";}
      else {document.all['layer_name'].style.visibility = "visible"
    }
    
    function hide(){
      if(document.getElementById) {document.getElementById("layer_name").style.visibility = "hidden";}
      else if(document.layers) {document.layers["layer_name"].visibility = "hide";}
      else {document.all['layer_name'].style.visibility = "hidden"
    }
    </script>
    </head>
    <body>
    <div id="layername" style="position:absolute;left:200;right:200">HELLO</div>
    <a href="#" onmouseover="show()" onmouseout="hide()">
    Mouse Here!
    </a>
    </body>
    </html>
    Note that for Netscape, the layer has to be absolutely positioned.

    Jon.

  6. #6
    SitePoint Member Slash's Avatar
    Join Date
    Sep 2000
    Location
    Egypt
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a more generic way to detect your browser, and also to join it with many more re-usable functions.

    Code:
    <script language="JavaScript">
    	// detect browser type ;
    function init(){
    	this.ver=navigator.appVersion
    	this.dom=document.getElementById?1:0
    	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
    	this.ie4=(document.all && !this.dom)?1:0;
    	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
    	this.ns4=(document.layers && !this.dom)?1:0;
    return this;	
    }
    bw=new init()			
    
    	// hide and Show funtions..
    function show(div){
    	obj=bw.dom? document.getElementById(div).style:bw.ie4?document.all[div].style:
    		bw.ns4?document.layers[div]:0;
    	if(obj) obj.visibility='visible'
    }
    
    function hide(div){
    	obj=bw.dom? document.getElementById(div).style:bw.ie4?document.all[div].style:
    		bw.ns4?document.layers[div]:0;
    	if(obj) obj.visibility='hidden'
    }
    </script>
    This type of code called Object Oriented DHTML. Which is generic and very usefull in hadling many cross-browser DHTML implementation. Define once, and forget about it.

    I've tried this code many times beacuse it was really the best I ever had. It is recommended for complex DHTML cross-browser scripting, but it can be used on simple pages too.
    Slash

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI, sorry if I am late replying....

    I tried using the above codes, but all two of 'em give me an error. Maybe it's because I use IE 6 beta, wich may have some bug. But it doesn't work anyway in netscape.

    I uploaded the page I was working on.
    I call the function with an argument in the first link at the left "manage it". Could someone please check and tell me what's wrong?
    http://www.tabssquare.com/aspkeeper/test.htm

    Thanks all for helping
    Last edited by tabsquare; Apr 28, 2001 at 06:15.

  8. #8
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to use quotes around the layer you are calling, because its a string, not a variable :

    onmouseover="show('first')"

    So put single quotes around the layer name...and similarly with onmouseout and hide.

    Jon.
    Last edited by jonsteele; Apr 28, 2001 at 09:50.

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you right, my mistake, but still doesn't work.....

  10. #10
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need quotes when calling the function in the onmouseover/onmouseout.

    But you don't need quotes in the actual function.

    function show (layername){ //no qutoes
    ...
    document.getElementById(layername)... //no quotes
    document.layers[layername]...//no quotes
    document.all[layername].... //no quotes
    ...
    }

  11. #11
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, I forgot to upload the right one...
    I think I tested everything about those quotes, but it still doesn't work...

  12. #12
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both your show and hide functions are missing a closing brace '}' for the if statement. Hopefully that should fix it.

    function show(layername){
    if(document.getElementById) {document.getElementById(layername).style.visibility = "visible";}
    else if(document.layers) {document.layers[layername].visibility = "show";}
    else {document.all[layername].style.visibility = "visible"}
    }

    function hide(layername){
    if(document.getElementById) {document.getElementById(layername).style.visibility = "hidden";}
    else if(document.layers) {document.layers[layername].visibility = "hide";}
    else {document.all[layername].style.visibility = "hidden"}
    }

  13. #13
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AH AH......AH AH.....it works!!! I AM GENIOUS!!! well maybe not. I works great now thanks. There is still something wrong in netscape, so that's why I hate netscape eh eh
    I bought a JS book, I think I will skip all chapters and go directly to the debugger software chapter
    Thanks a lot

  14. #14
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the netscape problem...

    You have the show/hide for the div's in the <div> tag. Try adding an <a> tag and putting it in there. Netscape doesn't support event handlers in the div.

    <a href="#" onmouseover="show('first')" onmouseour="hide('first')">
    <div id="first">
    ...
    </div>
    </a>

    Jon.

  15. #15
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI again

    mmm doesn't work, you sure you are talking about netscape 6? I have that one.
    The layers show up on mouse over, but it seems like if the images at the bottom on the layer don't show up. Is it possible that I have to preload them?

  16. #16
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Grosseto, Italy
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    oo alleluiaa!!!

    finally...finished.
    It works great now. Finally. I should test that also in Opera and in the others, but...that's for another day of my life

    Thanks


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
  •