Web ブラウザーをエミュレートするスクリプトを作成しました。ユーザーが上部の URL ボックスに何かを入力し、[go] を押すと、ページの中央に表示されます。ユーザーが新しい URL を入力すると、現在の URL が配列/スタックに格納されます。ユーザーが戻るボタンを押すと、アイテムがスタックから取り出され、現在の URL に表示されます。現在の URL は新しい URL になり、ユーザーは Web ページを先に進むことができます。
バックスタックと forStack がそれぞれ使い果たされたときに、Javascript を使用して「無効」属性を戻るボタンまたは進むボタンに設定したいと考えています。これを行うためにどのコードを使用するか考えていますか?
<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> </p>
</BODY>
</HTML>