Solved

Parent.Document Code - Why Won't This Work?

Posted on 2003-11-13
8
1,370 Views
Last Modified: 2012-06-27
Hi Folks,

I'm an ASP developer but I use Javascript a lot in my applications. The problem I'm facing here is using ASP to 'create' a javascript function programatically, as follows:

<SCRIPT LANGUAGE=JAVASCRIPT>
function SomeFunction() {
  <%=JavascriptCodeToUse%>
}
</SCRIPT>

This particular line of code is inserted into a 'popup' window. Now, what is supposed to happen is that when the user presses a button on the form of the popup window, it will insert an option in the listbox control of the window's opener (parent.opener?). At the moment, this will assign a static value for testing purposes but later it will assign the value of the selected control on the popup itself.

Here's the code I'm trying to use - it won't work so any help would be much appreciated:

<SCRIPT LANGUAGE=JAVASCRIPT>function ClickMe() {

var oOption = parent.opener.document.createElement('OPTION');

parent.opener.document.oOption.text='Sample';

parent.opener.document.oOption.value=3;

parent.opener.document.frmReport.more_students.add(oOption);

}

</SCRIPT>

Thanks very much!
0
Comment
Question by:icx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 15

Expert Comment

by:OMC2000
ID: 9738821
parent.opener references opener window of frameset from frame point of view or opener of main window referenced from iframe.
if you have single frame in your popup, refer to window.opener instead.
0
 
LVL 1

Author Comment

by:icx
ID: 9738837
Nope, still doesn't work.
0
 
LVL 15

Accepted Solution

by:
OMC2000 earned 155 total points
ID: 9738885
What happens when you call this function? Error message or just silence?
Also, it should look like:
<SCRIPT LANGUAGE=JAVASCRIPT>function ClickMe() {

var oOption = opener.document.createElement('OPTION');

oOption.text='Sample';
oOption.value=3;

opener.document.frmReport.more_students.add(oOption);

}

</SCRIPT>

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 1

Author Comment

by:icx
ID: 9738955
Well, it was silence as you called it, but I modified the code to look exactly as you described and it worked brilliantly.
The only thing I had different was that the lines referencing oOption.text and oOption.value were preceeded with window.opener.

Thanks very much, enjoy your points.
0
 
LVL 1

Author Comment

by:icx
ID: 9771507
Wait a moment ---- when the form is then submitted after using this code, the form submission (REQUEST object) doesn't contain anything inserted in to the form field using this code. Why is this?

Cheers,

Richard.
0
 
LVL 15

Expert Comment

by:OMC2000
ID: 9772051
In fact it's another question;)
Request object contains form elements and their values or query string if page called as URL with wuery parameters. If you have selected value newly inserted into more_students listbox, it will be value of  "more_students" request parameter.

If you want to have it selected right in ClickMe()  function, you should say it:

    si = document.frmReport.more_students.options.length - 1;
    if ( isNav ) // Netscape
      document.frmReport.more_students.options[si].selected = true;
    else // IE
      document.frmReport.more_students.selectedIndex = si;
0
 
LVL 1

Author Comment

by:icx
ID: 9772271
Hi OMC2000,

Thanks - will this select ALL the options in the listbox?
0
 
LVL 15

Expert Comment

by:OMC2000
ID: 9772490
this will select only last inserted option.

You can select all option in select with MULTIPLE attribute only.
Then you should iterate through all options in the list and set

document.frmReport.more_students.options[i].selected = true;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question