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

Discussion in 'Javascript' started by TPK, Feb 14, 2006.

  1. TPK

    TPK Guest

    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
     
    TPK, Feb 14, 2006
    #1
    1. Advertising

  2. TPK

    Guest

    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]);
     
    , Feb 14, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmV6YSBTb2xvdWtp?=

    Not able to populate and select an item in a list dynamically? Hel

    =?Utf-8?B?UmV6YSBTb2xvdWtp?=, Aug 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    368
    Scott Allen
    Aug 25, 2004
  2. QUASAR
    Replies:
    6
    Views:
    431
    QUASAR
    Jan 17, 2004
  3. Ann
    Replies:
    0
    Views:
    159
  4. Enjoy Life
    Replies:
    2
    Views:
    283
    Roland Hall
    Feb 23, 2005
  5. PC HUA
    Replies:
    2
    Views:
    427
    PC HUA
    May 19, 2004
Loading...

Share This Page