Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a site built with simple templates; header.tpl, navigation.tpl, body_home.tpl, body_about.tpl, body_anotherpage.tpl, etc. The navigation.tpl contains jQuery and is used to dynamically build a drop down navigation menu. When an element is clicked on the first drop down menu, the next is built based on what element was clicked. At some point there's no more drop downs and a variable is set, such as: var action = "dropdowncomplete". Now, in the body_*.tpl template files there's more jQuery which is ran when the action === "dropdowncomplete" evaluates to true. However, I don't know how to do this check with jQuery (nor with JS). Here's a simplified piece of code to illustrate how it works:

/* header.tpl (<head>) - setting this global variable */
var action = null;

/* navigation.tpl - for simplicity's sake, when link is clicked, the var is set */
$(document).ready(function() { 
$('a').live('click', function() { 
action = "dropdowncomplete"; 
}); }); 

/* body_*.tpl - this should be executed when the var is set, in this case when a link is clicked */
if(action === "dropdowncomplete") {
// do something
}

Note that all the above 3 pieces of JS/jQuery code are in separate script blocks!

Thanks.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

I think you're under the misapprehension that each <script> block is a separate scope. It isn't. If your code example above is an accurate reflection of your actual code, it should work, because action is declared globally, so the assignment is changing the global and the test is checking the global.

If it's not working, it's probably because you've established some other scope not shown above, like putting the var action = null bit inside a function.

share|improve this answer
    
Knowing it was probably a bug, I went through the code a few times and did find a "bug" in the code. The links have information in the href attribute that is used to generate the next drop down menu ( they contain information like foo=bar which is passed to a script with AJAX) so I have to use "return false" in the navigation.tpl at the end of the $('a').live('click', function() { }); block to prevent the link from redirecting to that url and that line breaks the execution of the code and so the following script blocks are not executed. Now I need a workaround for this problem. –  TheMagician Oct 13 '09 at 23:22
    
Best post it as a separate question. This one's answered. :) –  Warren Young Oct 13 '09 at 23:26

I would use an hidden input in my html to store the value

share|improve this answer
1  
or even use $.data() if using jQuery - docs.jquery.com/Internals/jQuery.data –  Russ Cam Oct 13 '09 at 22:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.