By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,304 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,304 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Feb 14 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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","widt h=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]);

Feb 14 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.