Solved

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

Posted on 2003-11-13
8
1,369 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
  • 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
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.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Jquery-Javascript setAttribute function help 7 18
how can i make 2 columns? 4 37
how can i get 4 nd 5 ? 3 31
Search Item in Table 2 19
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…

839 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