Wednesday, December 30, 2009

Opening a URL from a Select Form


This gets the value associated with the selected text (a URL) and opens the web document. Some details follow below.

First the HTML code for this:
<script type="text/javascript">
function openURL() {
 var selectionIndex = document.forms.urlChoose.elements["theURL"];
 var myURL = selectionIndex.options[selectionIndex.selectedIndex].value;
  document.location.href=myURL;
  return true;
}
</script>

<form action="" name="urlChoose">
<select gtbfieldid="23" name="theURL">
   <option selected="selected">Choose a Website</option>
   <option value='http://www.scriptsforapple.com/'>AppleScript Site</option>
<option value='http://read-and-write-german-language.blogspot.com/'>German Site</option>
<option value='http://editing-web-color-html.blogspot.com/2009/12/box-style-properties.html'>Color HTML Site</option>
</select>

<input name="submitURL" onclick="openURL()" type="button" value="Go to site" />
</form>


When a choice is selected and the button is clicked, 'onclick' sends a call for the 'openURL' function in the JavaScript (script) section.

The variable var selectionIndex is assigned a value of document.forms.urlChoose.elements["theURL"] . This gives it the value of the element named 'theURL' which is a URL of derived from the form named 'urlChoose' of the current document.

Next, the variable 'myURL' gets the value (a URL) of the index number 'selectionIndex' of 'selectionIndex' and opens the appropriate website.


Digg 'Learning How to Write JavaScript Code'!


Here what is shown on the page:






Please leave me a comment on this site.

For a look at using color with HTML go to: http://editing-web-color-html.blogspot.com/

No comments:

Post a Comment