PDA

View Full Version : Using var from js in html


DiaH
02-12-2003, 04:22 PM
I have this code at the top of my html page:

function window_onload() {
var strURL;
var strLowerURL;
var strMenuSwap;

strURL=document.referrer;

strLowerURL=strURL.toLowerCase();

alert(strLowerURL);

if(strLowerURL.search(/soh/)>-1)
{ //is public
strMenuSwap='../../soh/Scripts/menu_var.js';
}
else
{ //is internal
strMenuSwap='../../soh_internal/Scripts/menu_var.js';
}

}

I want to use the variable strMenuSwap in the html code where it calls the external js file. I thought I knew how to do this but I can't figure it out.

Any help is appreciated!!!
Dia

head8k
02-12-2003, 04:50 PM
Well you won't be able to do this onload because there would be no way of writing to the page as it compiled.

You should be able to do away with using a function and just put something like this in the <body>

<script language="javascript">
<!--
strURL=document.referrer;
strLowerURL=strURL.toLowerCase();

if(strLowerURL.indexOf("/soh/")!=-1)
strMenuSwap='<scr'+'ipt src="../../soh/Scripts/menu_var.js" type="text/javascript"></sc'+'ript>';

else
strMenuSwap='<scr'+'ipt src="../../soh_internal/Scripts/menu_var.js" type="text/javascript"></sc'+'ript>';

document.write(strMenuSwap);
//-->
</script>

It is always a good idea to split up the word 'script' when document.writing a javascript tag like this because it can cause problems in some browsers otherwise.