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

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!
LVL 1
icxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OMC2000Commented:
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
icxAuthor Commented:
Nope, still doesn't work.
0
OMC2000Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

icxAuthor Commented:
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
icxAuthor Commented:
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
OMC2000Commented:
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
icxAuthor Commented:
Hi OMC2000,

Thanks - will this select ALL the options in the listbox?
0
OMC2000Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.