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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global variable in a frames window

Status
Not open for further replies.

Vmusic

MIS
Dec 29, 1999
2
US
Hi,
I looked at some of the other global var threads( from a search query) they didn't seem to help.

I have a site that's using frames. There's a menu frame, a header frame and body frame. The menu and header frame have one html page that never changes. The body frame changes pages for different areas of the site. No biggy

EACH and EVERY page has this at the top:
<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;cthc.js&quot;>
So they are all using ONE single javascript -

At the top of this script I have what i want to be a Global variable used to track which page is currently loaded in the body frame
var current_area = '';

Now I have a function called ... of all things
function setCurrentArea(whosThereNow){
current_area = whosThereNow;
}

On any of the web pages that load into the body frame, I call this function to set the Global variable so it should know or be set to which ever page is currently in the body frame - something like:
<BODY BGCOLOR=&quot;#EDF6C9&quot; onLoad=&quot;setCurrentArea('body');&quot;>


-- Now here's the problem, in other functions I want to evaluate or test this variable to see what page is loaded in the frame -- BUT IT DOESN'T WORK

if (current_area == 'body'{
//do some cool stuff
}

It always is NULL or '' -

HOW HOW HOW Can I set a global variable with a function call from a page in the body frame - and really have it set globally - then when I call a different function from a
page in a different page it really is honestly set.

Thanks In Advance!!!! ~Just Say When~
 
The bad news:

Global variables are not really global in the way you are thinking. The scope is only within the page they are loaded in, and live only as long as the page is loaded.

The good news:

What you need to do is manipulate the global variables in one of the static frames. You could use either the menu, header, or better yet, the actual frame definition (parent) frame to store all the persistant vars.

If you used the parent frame, the code would change to:

function setCurrentArea(whosThereNow){
parent.current_area = whosThereNow;
}

Actually, I prefer to set vars inside their own scope, so the approach I would use would be to call the function in the parent frame from the current page:

<BODY onLoad=&quot;parent.setCurrentArea('body');&quot;>

And leave the setCurrentArea() function as is. The difference is the fact you would have to do this on every content page. Personally, I like to use an init() function on all my pages, and have that handle all the setup required for a page.

Anyway, when your page needs to use the variables, like in your example function, you simply access them like this:

if (parent.current_area == 'body'{
//do some cool stuff
}

Because these frames never change, the data will persist as long as the user doesn't leave. Using the parent frame ensures that an silly error in one of the other frames doesn't nuke your vars, since the user never actually interacts directly with the parent frame.

I had to do a lot of inter-frame stuff for several web based CBT courses I worked on, and it works pretty smooth once you get it down.

Hope it helps

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top