IF statements being skipped in javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
boberto
Forum Newbie
Posts: 4
Joined: Fri Oct 22, 2004 3:37 pm

IF statements being skipped in javascript

Post by boberto »

Here is my problem: I am using php to get a querystring from the url sent to my page, so it's something like index.php?menu=16. The menu integer is storing a bitwise value so that I can break down what menus should be expanded when they go to that page.

As you can see from my constants, a value of 1 means main menu is expanded, 2 means chop menu is expanded. A value of 3 would mean main AND chop menus are expanded.

The IF statements in the initMenuFlags function are my problem. No matter what combination of menus should be expanded, only the FIRST IF statement is being executed. For example, menu passes a value of 12. That means tutorial and beat menus are expanded. Only the IF statement for tutVal is being executed and therefore only that menu is being expanded. The script is completely ignoring the IF statement for beatVal.

What can I do to remedy this so that all IF statements are being evaluated? For those who aren't aware of what I am doing, imagine the menu value being in binary and being 5 bits. Originally it is 00000. Each bit acts as a toggle for a menu. Main menu on would be 00001. Tutorial menu on would be 00100. My if statements are checking if my menuflags variable contains the toggle for each menu.

Thanks for any help, its much appreciated!

Code: Select all

<?php if (isset($_GET&#1111;'menu'])) $menuFlags=$_GET&#1111;'menu']; else $menuFlags=0; ?>

<script language="javascript1.3">
const mainVal=1;
const chopVal=2;
const tutVal=4;
const beatVal=8;
const linkVal=16;

function redirectURL(myURL) &#123;
 //alert("menuflags " + menuFlags);
 myURL = myURL + "&menu=" +  menuFlags;
 window.location=myURL;
&#125;
function initMenuFlags() &#123;    
	menuFlags=<?php echo $menuFlags ?>;
	if(menuFlags & mainVal)
		neonixToggle('subMain','mnuMain','',false,'Main', 0);
	if(menuFlags & chopVal)
		neonixToggle('subChopShop','mnuChopShop','',false,'Chop Shop', 0);
	if(menuFlags & tutVal)
		neonixToggle('subTutorial','mnuTutorials','',false,'Tutorials', 0);
	if(menuFlags & beatVal)
		neonixToggle('subBeats','mnuBeats','',false,'Beats', 0);
	if(menuFlags & linkVal)
		neonixToggle('subLinks','mnuLinks','',false,'Links', 0);
					
&#125;

function neonixToggle(itemID,linkID,anchorID,tSwitch,tText, menuValue)&#123;// Neonix Toggle Ver 3.0.1 - &#1111;url]www.neonix.net&#1111;/url]
	if (document.getElementById && navigator.userAgent.indexOf('Opera') == -1)&#123;
		var itemEL = document.getElementById(itemID);
		var linkEL = document.getElementById(linkID);
		itemEL.className = itemEL.className == 'TG_visible' ? 'TG_hidden' : 'TG_visible';
		if(itemEL.className=='TG_visible') menuFlags+=menuValue;
		else menuFlags-= menuValue;
		if(!eval(tSwitch))&#123;linkEL.innerHTML = itemEL.className == 'TG_hidden' ? '> ' + tText : '< ' + tText;&#125;
		//alert("mv. " + menuValue + " mf. " + menuFlags);
	&#125;
	if (anchorID.length != 0)&#123;;
		document.location.href = '#' + anchorID;
	&#125;
	return;
&#125;
</script>
boberto
Forum Newbie
Posts: 4
Joined: Fri Oct 22, 2004 3:37 pm

Post by boberto »

I have found that by sticking alert statements like this, everything works flawlessly. It's like adding in that pause makes every if be evaluated. Is there a better way to add pauses in that won't pop up windows on my page?

Code: Select all

if(menuFlags & mainVal)
      neonixToggle('subMain','mnuMain','',false,'Main', 0);
   alert("main if done");
   if(menuFlags & chopVal)
      neonixToggle('subChopShop','mnuChopShop','',false,'Chop Shop', 0);
   alert("chop if done");
   if(menuFlags & tutVal)
      neonixToggle('subTutorial','mnuTutorials','',false,'Tutorials', 0);
   alert("tut if done");
   if(menuFlags & beatVal)
      neonixToggle('subBeats','mnuBeats','',false,'Beats', 0);
    alert("beat if done");
   if(menuFlags & linkVal)
      neonixToggle('subLinks','mnuLinks','',false,'Links', 0); 
    alert("link if done");
Post Reply