script to open a new window and populate it with a single item from a list

T

TPK

Here is what I want to do with javascript.

On a page with text place a javascript link that:

1) When a user clicks the link (onClick) a new browser window opens
(the easy part) NewWindow =
window.open("windowpage.html","newWin","width=100,height=100")

2) Once the window is open I want to pull a specific item from a list
of items (array) and populate the open window with only that item. The
list of items would be in a separate .js file. Call it "definitions.js"

3) The (example) list of items (on definitions.js) has a list of terms
and definitions:

Example List:
TermArray = new Array();
DefinitionArray = new Array();

TermArray[0] = "Stop Sign"
DefinitionArray[0] = "Stop Sign: A red sign with the word STOP.";

TermArray[1] = "Yield Sign"
DefinitionArray[0] = "Yield Sign: A yellow sign with the word YIELD.";

TermArray[2] = "Go Sign"
DefinitionArray[0] = "Go Sign: A green sign with the word GO.";

TermArray[3] = "Right Turn Sign"
DefinitionArray[0] = "Right Turn Sign: A white sign with an arrow
pointing RIGHT.";

4) The new browser window can then be closed by clicking a
"javascript:window.parent.close()" link.

So basically I'd like to pass a variable, but I'm not exactly sure
how structure the script on the referring page/link.
Can anyone send me a link to a resource that would describe doing this
or can show me the script?

Thank you,
TPK
 
S

somejeff

The cleanest way to pass a variable is to send it as part of the URL.

for example, your link would be:
<a href="#" onclick="describe('Stop Sign');">Stop Sign</a>

your javascript on the main page would be:

function describe(term) {

window.open("windowpage.html?"+term,"newWin","width=100,height=100")
}

(essentially the URL of the window that pops open would be
"windowpage.html?Stop%20Sign")

The scripting in that window, looks for the term after the '?' in the
URL like this:

var term = location.href.substring(location.href.indexOf("?")+1);
// essentially everything the "?"

then look up the term in your array.

By the way, your definitions arrays would probably work out better if
they were like this instead:

DefinitionArray = new Array();
DefinitionArray["Stop Sign"] = "Stop Sign: A red sign with the word
STOP.";
DefinitionArray["Yield Sign"] = "Yield Sign: A yellow sign with the
word YIELD.";
DefinitionArray["Go Sign"] = "Go Sign: A green sign with the word GO.";
DefinitionArray["Right Turn Sign"] = "Right Turn Sign: A white sign
with an arrow pointing RIGHT.";

That way you can then use:

document.write(DefinitionArray[term]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top