Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling JS functions from a different JS functions If statement

Status
Not open for further replies.

MYQUE

Technical User
May 22, 2002
25
GB
Hi,

I'm trying to display different layers depending on what browser the user is using. I have one Javascript Function that shows/hides a layer and another which detects the bowser type and then is supposed to call the JS layer function accordingly. Needless to say, I havn't really got a clue what to edit and can't figure it out. If anyone could have a look at the code I'm using below and show me what it should look like I would be most gratefull.

Thanks for your time.

---------
THE CODE:
---------

This is the script that toggles the layers between visible and hidden:

<script language=&quot;JavaScript&quot;>
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
obj.visibility=v; }
}
//-->
</script>


This is the script I have mangled in the process of trying to get it to call the layer script according to the users browser type. I have no idea how to call Javascript function from within another Javascript function, this is my first hurdle. Please help! :)

<script language=&quot;JavaScript&quot;>
var browser_type=navigator.appName
var browser_version=parseInt(navigator.appVersion)

//if NS 6
if (browser_type==&quot;Netscape&quot;&&browser_version>=5)
eval ('MM_showHideLayers('NS6','','show')') &&
eval ('MM_showHideLayers('NS4','','hide')') &&
eval ('MM_showHideLayers('IE','','hide')');
//if IE 4+
else if (browser_type==&quot;Microsoft Internet Explorer&quot;&&browser_version>=4)
eval ('MM_showHideLayers('NS6','','hide')') &&
eval ('MM_showHideLayers('NS4','','hide')') &&
eval ('MM_showHideLayers('IE','','show')');
//if NS4+
else if (browser_type==&quot;Netscape&quot;&&browser_version>=4)
eval ('MM_showHideLayers('NS6','','hide')') &&
eval ('MM_showHideLayers('NS4','','show')') &&
eval ('MM_showHideLayers('IE','','hide')');
//Default action to take (NOT NS 4+ and NOT IE 4+)
else
eval ('MM_showHideLayers('NS6','','hide')')&&
eval ('MM_showHideLayers('NS4','','hide')')&&
eval ('MM_showHideLayers('IE','','show')');
</script>

 
your question is not really clear. You have all the code already done ! it IS calling the MM_showHideLayers and the MM_findObj(n, d) functions ... It is hiding/shwoing frames depending on the browser type/version ... so where exactly is your problem ? why do you want to edit this code, what is your goal ?
 

It doesn't work when I test it though. I get the following error: Line: 38 Error: Expected ')'

Hope this makes it more clear as to what I'm trying to do: I have three layers named NS4, NS6, IE. I wish to display the correct layer depending on which browser the user is viewing the page with. So if the user is using Internet Explorer the IE layer is made visible and the other two kept hidden, if the user is viewing the page with Netscape 4+ but not verion 6 the the layer NS4 is made visible and the rest left hidden. The problem I'm having is getting the show/hide layer script to execute from within the browser detection script. I've basically been trying to look at other codes to see how they execute script functions from within other script functions but I've had no luck getting them to work. I'm basically guessing and have next to no knowledge of Javascript.

I've pasted the whole pages code below so you can test it as well if you like and have a close look. Thanks for your help.

----
CODE
----

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script language=&quot;JavaScript&quot;>
<!--
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName==&quot;Netscape&quot;)&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// -->

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
obj.visibility=v; }
}
//-->
</script>

<script language=&quot;JavaScript&quot;>
var browser_type=navigator.appName
var browser_version=parseInt(navigator.appVersion)

//if NS 6
if (browser_type==&quot;Netscape&quot;&&browser_version>=5)
eval ('MM_showHideLayers('NS6','','show')') &&
eval ('MM_showHideLayers('NS4','','hide')') &&
eval ('MM_showHideLayers('IE','','hide')');
//if IE 4+
else if (browser_type==&quot;Microsoft Internet Explorer&quot;&&browser_version>=4)
eval ('MM_showHideLayers('NS6','','hide')') &&
eval ('MM_showHideLayers('NS4','','hide')') &&
eval ('MM_showHideLayers('IE','','show')');
//if NS4+
else if (browser_type==&quot;Netscape&quot;&&browser_version>=4)
eval ('MM_showHideLayers('NS6','','hide')') &&
eval ('MM_showHideLayers('NS4','','show')') &&
eval ('MM_showHideLayers('IE','','hide')');
//Default action to take (NOT NS 4+ and NOT IE 4+)
else
eval ('MM_showHideLayers('NS6','','hide')')&&
eval ('MM_showHideLayers('NS4','','hide')')&&
eval ('MM_showHideLayers('IE','','show')');
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>

<div id=&quot;IE&quot; style=&quot;position:absolute; left:53px; top:47px; width:161px; height:124px; z-index:1; background-color: #CC0000; layer-background-color: #CC0000; border: 1px none #000000; visibility: hidden&quot;><font color=&quot;#FFFFFF&quot;>IE</font></div>
<div id=&quot;NS4&quot; style=&quot;position:absolute; left:249px; top:88px; width:178px; height:134px; z-index:2; background-color: #CCFF00; layer-background-color: #CCFF00; border: 1px none #000000; visibility: hidden&quot;><font color=&quot;#CC0000&quot;>NS4+</font></div>
<div id=&quot;NS6&quot; style=&quot;position:absolute; left:442px; top:45px; width:201px; height:143px; z-index:3; background-color: #66CCFF; layer-background-color: #66CCFF; border: 1px none #000000; visibility: hidden&quot;><font color=&quot;#000033&quot;>NS6</font></div>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top