Categories:

Location.search

A location.search string will be formatted according to certain rules. For instance, every specific variable contained in the string will be a statement in itself, separated from each other by ampersand "&" characters. Therefore, we can split the location.search string into an array of strings, like this:

mySearch = location.search.substr(1).split("&")

This creates an array of objects named mySearch. Each will look like this:

myValue=AEIOU

From this, we can use a for loop and eval statements to set certain values of the form:

function getFromSearch() {
var x = 0
mySearch = location.search.substr(1).split("&")
for (x=0;x<=mySearch.length;x++) {
eval("document.forms.myNewForm."+mySearch[x])
}
}

The results of this should be displayed in the text box below.

Popup windows

This is a very versatile method of sending information from one page to another. The idea is that each window is an object we can store other objects in. This includes objects with specific properties and methods, instead of simple strings (allowed in cookies and in the location.search property). For instance, click on this to create a brief popup window.

What you just saw was a simple copy function, executed onLoad:

function initForm() {
document.forms.popupForm.MyValue.value = opener.document.forms.myNewForm.MyValue.value
document.forms.popupForm.MyTest.value = opener.document.forms.myNewForm.MyTest.value
}

However, more is possible. If you click this you should see an alert box describing an object holding three properties: a first property, a second property, and a third property. (Red, green, and blue are their string contents.)

Basket = new Object()
Basket.first = "red"
Basket.second = "green"
Basket.third = "blue"

For a popup window, a simple way to transfer the object is to simply assign it to the second window, and then recall it on the next page.

Click on this to restore the popup window (do NOT close it then!). Then click this button to simulate a event. (The results would be identical if we actually did submit the form.)