2 Replies - 470 Views - Last Post: 21 January 2013 - 10:15 AM

#1 redshine6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 13

Locking buttons when array is empty

Posted 21 January 2013 - 07:34 AM

I have written a script that emulates a web browser. The user enters something in the URL box at the top, when they press go, it is displayed in the center of the page.
If the user enters a new url, the current url is stored in an array/stack. When the user presses back, the item is popped off the stack and displayed in the current url, and the current url goes into a new url allowing the user to click forward through the web pages.

However, when the backStack array is empty I don't want the user to be able to click back any more, and vise versa for the forStack array. Any idea how I can do this?



<HTML>
<HEAD>
<TITLE>Stacking up!</TITLE>
<script>


var backStack = new Array(); 
var forStack = new Array();
var curUrl = "";

function pushStack(newVal) {
	if (curUrl == "") 
	curUrl = newVal;
	else
   backStack.push(curUrl);
   curUrl = newVal;
}

function pushForStack(newVal) {
	forStack.push(curUrl);
	curUrl = newVal;
}

function popBackStack() {
   var popVal = backStack.pop();  
   if (popVal == undefined)
      return "Nothing left!";
   else
   return popVal
}

function popForStack() {
   var popVal = forStack.pop();
   if (popVal == undefined)
      return "Enter a new URL";
   else
   return popVal;
}

function showStack(theSelect){
   theSelect.options.length = 0;
   for (var i = 0; i < backStack.length; i++){
      var theOption = new Option(backStack[i]);
      theSelect.options[theSelect.options.length] = theOption;
   }
}

function showStack2(theSelect){
   theSelect.options.length = 0;
   for (var i = 0; i < forStack.length; i++){
      var theOption = new Option(forStack[i]);
      theSelect.options[theSelect.options.length] = theOption;
   }
}
</SCRIPT>



</HEAD>
<BODY>
<FORM>
<table width="104%" height="364" border="5" cellpadding="3" cellspacing="3">
  <tr>
    <th width="30%" height="78" scope="col"><p>
    <INPUT type=button value="Back" onclick='txtPop.value = popBackStack(); pushForStack(txtPop.value); showStack(theList); showStack2(theList2);'></p></th>
    <th width="46%" scope="col"><p>
  <center>
  <INPUT type=text name="txtPush">
  <INPUT type=button value="Push" onclick='forStack.length=0; showStack2(theList2);pushStack(txtPush.value);txtPush.value="";txtPop.value = curUrl; showStack(theList);'>
  </center>
  </p></th>
    <th width="24%" scope="col"><p><INPUT type=button value="Forward" onclick='txtPop.value = popForStack(); pushStack(txtPop.value); showStack(theList); showStack2(theList2);'></p></th>
  </tr>
  <tr>
    <td><p><center>
    <SELECT name="theList" size=12>
    </SELECT>
    </center></p></td>
    <td><p><center><INPUT type=textki name="txtPop" id="txtPop" size=25></center></p></td>
    <td><center>
    <SELECT name="theList2" size=12>
    </SELECT>
    </center></td>
  </tr>
</table>
</FORM>
<p>&nbsp;</p>
</BODY>
</HTML>




Is This A Good Question/Topic? 0
  • +

Replies To: Locking buttons when array is empty

#2 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Locking buttons when array is empty

Posted 21 January 2013 - 08:17 AM

You could use Javascript to set the 'disabled' attribute on button when stack is exhausted.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,845
  • Joined: 12-December 12

Re: Locking buttons when array is empty

Posted 21 January 2013 - 10:15 AM

 onclick='forStack.length=0; showStack2(theList2);pushStack(txtPush.value);txtPush.value="";txtPop.value = curUrl; showStack(theList);'


Running several functions like this doesn't give you the control that you need. It will call all the functions in sequence regardless of whether you need to stop the process.

Each button-click should call only one function. That function can call other functions if necessary, and these functions would return something such as the value false to indicate that it cannot proceed.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1