10.2 Keeping a Page Out of the Browser HistoryNN 3, IE 4 10.2.1 Problem
You want to remove the current page from the browser history so that the Back button does not take the
10.2.2 Solution
Navigate to the
location.replace("http://www.megacorp.com/indexDHTML.html");
10.2.3 Discussion
The capability of keeping a page out of the browser's history can come in handy when your site contains a page that include automatic forwarding under script control. Recipe 5.10 is a typical situation that benefits from keeping a temporary page out of the history. If the user
You can also use this technique if you do not want a user to come back to a form after submitting it. But in this case, you must assemble the form data yourself, appending it to the URL string passed with the
replace( )
method. The page returned from your CGI program
10.2.4 See AlsoRecipe 5.10 for a likely scenario for using location.replace( ) ; Recipe 10.6 to see how to assemble form data as part of a URL. |
10.3 Using a select Element for NavigationNN 2, IE 3 10.3.1 ProblemYou want users to choose a destination from a pop-up list originating from a <select> tag. 10.3.2 SolutionYou have a few scripting possibilities for this solution, depending on your design and scripting style, but they all rely on the select element having been outfitted with option elements containing the URL for each destination. You can display any text you like that is visible in the list, but assign the URL for each item to the value attribute of each option :
<select name="chooser" id="chooser">
<option value="">Choose a Destination:</option>
<option value="http://www.megacorp.com/index.html">Home</option>
<option value="http://www.megacorp.com/products/index.html">Products</option>
<option value="http://www.megacorp.com/support/index.html">Support</option>
<option value="http://www.megacorp.com/contact.html">Contact Us</option>
</select>
Some event must trigger the navigation action. The most
function navigate( ) {
var choice = document.forms[0].chooser;
var url = choice.options[choice.selectedIndex].value;
if (url) {
location.href = url;
}
}
Perhaps more
function navigate(choice) {
var url = choice.options[choice.selectedIndex].value;
if (url) {
location.href = url;
}
}
The event handler in the
select
element should be as
<select name="chooser" id="chooser" onchange="navigate(this)">...</select> 10.3.3 Discussion
The myriad of ways to translate a
select
element's choice into a navigation command depend primarily on the browser versions you need to support and your coding style, particularly with respect to event processing. For example, in IE 4 or later and NN 6 or later, you can access the
value
property of the
function navigate(evt) {
evt = (evt) ? evt : ((event) ? event : null);
if (evt) {
var elem = (evt.target) ? evt.target : ((evt.srcElement) ?
evt.srcElement : null);
if (elem && elem.tagName.toLowerCase( ) = = "select" && elem.value) {
location.href = elem.value;
}
}
}
You can then bind the event handler within the tag: <select name="chooser" id="chooser" onchange="navigate(event)">...</select> Or in a script statement that executes after the page loads:
document.getElementById("chooser").onchange = navigate;
The scheme presented thus far has a significant flaw, however. If the
To force the user to make a selection that fires the onchange event, script the page to restore the select element (or perhaps all form controls) to its default state either via the onload or onunload event for the document or window objects. Either invoke the reset( ) method of the containing form element object, or set the selectedIndex property of the select element to zero. Notice that the select element shown in the Solution contains an initial choice that has no value—just a text label with instructions. All of the navigation event handler functions shown here assign a value to the location.href property only if there is, indeed, a value (other than an empty string) associated with the selected option.
You may
10.3.4 See Also
Recipe 7.2 and Recipe 7.3 for controlling navigation of other
|