0

Maybe a closure is my solution? Not exactly sure how to pull it off though.

The code is set up like so:

var globalVar = '';
var globalVar2 = '';

function func()
{
   if (condition)
     func2(globalVar) 
   else 
     func2(globalVar2)
}

In func2() I cache some HTML in a main container into the appropriate global variable that I pass to it. Basically I have a main container that holds different pages depending on what tab they choose. For performance I want to cache the page into global vars so I need to know what tab is active to figure out which global var to assign the HTML to.

2 Answers 2

1

as globals are just properties of the window object in javascript, just pass "globalVar" or "globalVar2" and assign it using window[myvar] = ...

Sign up to request clarification or add additional context in comments.

Comments

0

Might I suggest simply adding these cached pages to the dom and hiding them using CSS rather than trying to hold onto them in JS and constantly change the main content container?

4 Comments

That's not really necessary either. I would just use AJAX and load the pages from file.
But, then you would be having a new httprequest every time you changed content, which is what caching prevents. This way you would at most load each page once. Your way you could load the pages infinitely many times. A bit of an exaggeration, but you get the point.
the main container is loaded via ajax when someone clicks on a tab. at that moment i cache the main container html, put a ajax loader animated gif then make an ajax call to the server. when the call returns i load that html into the main container. i don't want to have to hit the server with an ajax request every time someone tabs back and forth. so it's one call, then cached on every next tab click. to cache all the pages in the dom and hide them would be a bad idea. i'd have to load all 6 tab contents on the page load, which is not needed.
Just load them as they are called, not on page load. Initially you would load just one. Then as the person tabs around, rather than caching in javascript, cache in the dom (hiding the containing dom element), that way you don't have to add the cached content back to the dom, you can just reveal it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.