Thread: Simple JavaScript Code Issue

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    Simple JavaScript Code Issue

    I've been through Java, C, C++, and then XHTML. This website's boards are the best help on the web in my opinion. The people that helped me with C and C++ were the BEST. I am hoping there are some of you out there that know enough if JavaScript to help me here, too.

    Now I am struggling with this class, it is an online...more like self taught...class and the text book is bad.

    I have four coding assignments left, thank goodness. The one I am working on now is dealing with popup windows and cross-window communication. I have most of it written except for two things. The first is referencing user entered text from the original window in the popup window. The last is writing a line of text in the original window when the popup window is closed.

    If anyone has any ideas I would appreciate the assistance:

    This is 3a.htm (original window)
    Code:
    <html>
    <head>
    <script type='text/javascript'></script>
    </head>
    
    <body>
    <form>
    Name: <input type='text' name='name' /><br><br>
    Phone #: <input type='text' id='cPhone'/><br><br>
    Address: <input type='text' /><br><br>
    How Many Pizzas: <input type='text' id='pOrdered' /><br><br>
    <input type='button' onclick=window.open('3b.htm') value='Order'/>
    </form>
    </body>
    </html>
    and this is 3b.htm (the popup window)
    Code:
    <html>
    <head>
    <script type='text/javascript'>
      var noPizza = document.getElementById('pOrdered');
      var phoneNo = document.getElementById('cPhone');
    </script>
    </head>
    
    <body>
    You have ordered [no of pizza.value here] pizza(s)
    <br>
    You will receive a call shortly at phone number [phone number value here]
    <br><br>
    <input type='button' onclick=window.close() value='Confirm Order' />
    <input type='button' onclick=window.close() value='Cancel' />
    </body>
    </html>
    Thanks!
    Cindy

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If you rewrite 3a.html to dynamically create 3b.html (with document.write), the problem goes away.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    window.opener is another property that solves your problem. You can access 3a.html's named elements through this property.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I did write a function in 3a that created everything I needed in 3b. and I got all excited. Then I couldn't figure out how to get the buttons (confirm order and cancel) to work in that function. AND...the assignment said to create two html pages. So I got discouraged and started all over.

    I have been referencing the information on w3schools website for all the DOM properties and methods. I read and reviewed the opener property but couldn't figure out how to apply it. So, I kept on looking.

    Citizen, if you can' give a little more explanation that would be great.

    Thanks to you both,
    Cindy

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by clegs View Post
    Citizen, if you can' give a little more explanation that would be great.

    Thanks to you both,
    Cindy
    opener is just a reference to the window that opened that window.

    You use it like you would any other:

    Code:
    var pizza = opener.document.getElementById('pOrdered').value
    var phone = opener.document.getElementById('cPhone').value

  6. #6
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks!

    I'll try it and see if I can get it to print out.

    Then I have to print a line of text...'Order Submitted'...in 3a when the user clicks the 'Confirm Order' button in 3b. There was a hint to write a function in 3a that displays the message when invoked from the popup. Any ideas on that one?

    I appreciate your help. I have been working on this little bit of code all day. The 'textbook' for the class is 'PPK on JavaScript' he knows his stuff, but for an intro text, is isn't very comprehensive.

    Thanks again!
    Cindy

  7. #7
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I got everything to work except writing "Order Submitted" in 3a when the "Confirm Order" button is pushed in 3b.

    I have tried a few things and will keep trying but I am missing the logic on how to make it print.

    Thanks for your help citizen, using "opener" worked perfectly.

    Cindy

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe there's a property window.parent or such that would refer back to the parent window (opener).
    Also, if you open a window, you can store the window, ie:
    var mywindow = open("mywindow.html");

    Now mywindow is that window and you can manipulate it via this variable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Elysia, variables don't work across pages, FYI. You would need to use function parameters or somesuch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C++ question. Error in code somewhere.
    By Paracropolis in forum C++ Programming
    Replies: 10
    Last Post: 02-06-2006, 08:59 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM