Results 1 to 5 of 5

Thread: Passing values from one window to another

  1. #1
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing values from one window to another

    Hi there,

    I am fairly new to Javascrpit. I am trying to send a certain string variable from the current page into a new page (pop up).

    Currently the code below simlpy opens up "newpage.asp" in another window.
    That's great.

    Now I need to take a value and say place it in a text box in "newpage.asp".



    Code:
    <head>
    <script type="text/javascript" >
    
    function open_a_window_func()
    {
    
        open_str = "newpage.aspx";
        screen_size ="height=550,width=330,top=60,left=90,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no" 
    ;
        x = window.open(open_str,'window_name_eg_myNewWindow',screen_size);
        x.focus();
    }
    
    </script>
    </head>
    
    <body>
     <form id="formcheck" onsubmit="open_a_window_func(this);">
    
      </form>
    </body>


    Can anyone give me a few ideas?

    Thank you

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    HTML Code:
    <html>
    <head>
    <title>A New Window</title>
    <script type="text/javascript">
    // global variable for subwindow reference
    var newWindow;
    // generate and fill the new window
    function makeNewWindow( ) {
        // make sure it isn't already opened
        if (!newWindow || newWindow.closed) {
            newWindow = window.open("","sub","status,height=200,width=300");
            // delay writing until window exists in IE/Windows
            setTimeout("writeToWindow( )", 50);
        } else if (newWindow.focus) {
            // window is already open and focusable, so bring it to the front
            newWindow.focus( );
        }
    }
    function writeToWindow( ) {
        // assemble content for new window
       var k = 90;
        var newContent = "<html><head><title>Secondary  Window</title></head>";
        newContent += "<body><h1>This is a script-created window.</h1>";
        newContent += "<scr"+"ipt type='text/javascript' language='javascript'> var localVar; localVar = "+ k +"; alert('Value stored in local variable of the new window is '+localVar+'.'); document.write('localVar value: '+localVar);s</scr"+"ipt>";
        newContent += "</body></html>";
        // write HTML to new window document
        newWindow.document.write(newContent);
        newWindow.document.close( ); // close layout stream
    }
    </script>
    </head>
    <body>
    <form>
    <input type="button" value="Create New Window" onclick="makeNewWindow();" />
    </form>
    </body>
    </html>
    Checkout the writeToWindow( ) to know how you can pass variables to the new popup window.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Or, alternatively:
    Code:
    x.document.forms['form_name'].elements['text_box'].value = "new_value";
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks CodeExpoliter and Twey, I'll give these suggestions a bash!

  5. #5
    Join Date
    Aug 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post appending code to another page.

    Hi codeexpoilter,

    In your function writetowindow you add the HTML and Java script in page 1 which will be used in the new page.

    What if I want to open an existing page say 'page2.html'? How do I add your script to an existing page without overwriting the existing page?

    I want to take the variable vlaue and then use them with my controls on page 2.



    Code:
    function writeToWindow( ) {
        // assemble content for new window
       var k = 90;
        var newContent = "<html><head><title>Secondary  Window</title></head>";
        newContent += "<body><h1>This is a script-created window.</h1>";
        newContent += "<scr"+"ipt type='text/javascript' language='javascript'> var localVar; localVar = "+ k +"; alert('Value stored in local variable of the new window is '+localVar+'.'); document.write('localVar value: '+localVar);s</scr"+"ipt>";
        newContent += "</body></html>";
        // write HTML to new window document
        newWindow.document.write(newContent);
        newWindow.document.close( ); // close layout stream

    Cheers for your help

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •