Hello,
I have an agent that is processing a webform, and on completion, a printwriter is used in the java agent code to direct the agent to a URL: http://localhost/survey.nsf/Question6. This form, Question6, is the last step in a web survey, and on clicking a button on this form, the user invokes javascript code window.close() to attempt to close the form.
However, this message is displayed in the error console with Mozilla 2.0:
Scripts may not close windows that were not opened by script.
This error is happening because a script did not open the window, and hence it cannot be closed using window.close().
I tried to use window.location through javascript code to specify the url, but window.close only works when window.open has been used to open a window. If I use window.open(), the Question6 web form opens in another window, and the window.close() method works fine in this case, but I can still see the other form that was Question5 open. The idea of the survey is to move from Question to Question, and each time the window of the previous question should be closed.
Any help on how to close the original window, i.e. the Question5 form, that was opened with agent code will be appreciated.
Many thanks,
Marise
Subject: Agent code and javascript to close browser window
Hey there Marise.The problem you are having is due to built-in security features of modern browsers. The simple fact is that unless your Javascript code on the client browser has opened the window there is no way for you to programatically close the window.
However all is not lost. There are some workarounds you can try.
I would suggest that you start with using a $$Return field to redirect the browser to a new page, such as a “Thank you for your submission” (with a “Close this window” button); or the main page for your website.
Hope this helps!
-Devin.
Subject: RE: Agent code and javascript to close browser window
Hi Devin,
Thanks for your reply. However, when the form is submitted, an agent is invoked through javascript. So using the $$Return field does not work(unless I am not using it correctly), I placed the URL value in the field, but the document submission actually results in an agent call, not a form submission.
Even if I do use the $$Return field as you suggest to redirect to a new page, I don’t see how the close button would work, because again, the page has been opened with code other than javascript and so, cannot be closed with javascript.
I am able to open and close the Question6 window using window.open and window.close respectively. But I wanted to know how I can close the existing Question5 window, which was opened by using a printwriter and writing the URL to the browser, in agent code.
Please help! This is driving me insane 
Thanks,
Marise
Subject: Agent code and javascript to close browser window
Do you have to use javascript to close the window? How about having the button on form Question6 do @Command([FileCloseWindow])?
Subject: RE: Agent code and javascript to close browser window
Thanks Mike, I tried that and it works.