new window, close window and refresh parent

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

new window, close window and refresh parent

Post by phice »

I've got a forum I'm making that when you click a link, it opens a slightly formatted window, allows the user to post, and click a link to close the window.

But, the problem is that I need the parent window (where the user clicked the link to open the new, formatted window) to refresh to a page I request.

Any way to go about doing this? I've seen it done a few times, but not exactly sure how I'd do such a thing.
Image Image
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

opener.location.href='yourURL';
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

I am trying to do the same thing.
I have had success with this, but ONLY IN IE! (Arrrg)
In the head of the popup:

Code: Select all

<script language="JavaScript" type="text/JavaScript">
<!-- 
function SubmitForm(theForm) &#123;
 var parent_ref = opener;
 parent_ref.name = 'main';
 theForm.target = 'main';
 window.close();
&#125;

// -->
</script>
And in the form tag:

Code: Select all

<form action="YourScript.php" method="post"  onSubmit="return SubmitForm(this);">
And in the Opener (original) page

Code: Select all

<script language="JavaScript" type="text/JavaScript">
<!--
// main window name
window.name = 'main';
//-->
</script>
I need to get this to work in NS6-7 as well.
It must need a small workaround added.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

I've got the submit button going to another page in the same window. I need the code for a <a tag, which will close the window, then refresh the main page to the specified page.
Image Image
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

anyone?
Image Image
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

To submit to the parent window, put this in the head of the child window:

Code: Select all

<script language="JavaScript" type="text/JavaScript">
<!-- 
self.opener.name = 'main';


// -->
</script>
Then the form tag needs to have the parent window instance name as the target:

Code: Select all

<form name="form1" method="post" action="parentPage.php" target="main">
You can find a close window snippet onLine all over the place.
As far as closing the window after submit, it won't happen. NS cancels the submit and closes the window.

Sorry I can't be of more help, gotta' go.

dstefani
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Just to clarify any misunderstanding:


The main page is viewed, the user decides to reply to the post, and a new window opens. The form is inside the new window. The user inputs whatever it needs, hits submit, and page is loaded stating "Post has successfully been inserted...", followed by a link "Close window", which will close the window when clicked.

What I need is for the last link to refresh the Main page and close the small window.
Image Image
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

So when you say "refresh the main page" you mean, display the information just sent from the form in the popup window?

That's the easy part. The code I sent you in my last reply does just that.

You'll need to put a close button in your popup for the user to manually close the popup form page. You cannot (to my knowledge) do a submit and a close window in the same onSubmit action. I did a bunch of research on just this problem 2 weeks ago.

Test your close window button on a Mac, it doesn't work the same as on PC's. I'm sure you can find that info online.

I hope this is relevent.

dstefani
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Ok, I just need a link that will close the window (brought up by the main window), and make the main window go to another page.
Image Image
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post by dstefani »

<form>
<input type=button value="Close Window" onClick="javascript:window.close();">
</form>
or <a href="javascript:window.close();">Close Window</a>

I don't know if this will work on Mac.

d
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

I've already had the close window javascript code.

I think I'm heading in the right direction:

Code: Select all

<a href=index.php onClick="javascript:window.close();" target="main">
It does what it's supposed to do, except it opens a new window, instead of making the main window go to "index.php". Is there something I need to do to the main window to name it, so I would send the target location directly to that window.
Image Image
Skyzyx
Forum Commoner
Posts: 42
Joined: Fri Feb 14, 2003 1:53 am
Location: San Jose, CA

Post by Skyzyx »

window.close() DOES work on any browser that properly supports JavaScript 1.3 (which is most these days).

You need to do a timeout for the window close.

Code: Select all

function closeWin()
&#123;
	setTimeout("window.close();", 2000);
&#125;
... Then...

Code: Select all

<input type="button" value="Close Window" onclick="closeWin();">
Remember to be a good little web coder. Always quote your values, and don't use the "javascript:" pseudo-protocol in event handlers like "onclick". Also event handlers like "onclick" and "onsubmit" should be lowercase if you ever want to validate your pages as XHTML.

Have a nice day!
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Thanks for the tips :)

What can I do with the parent window to identify it? In example:

Code: Select all

<body name="parent">
..so that any target pointed to "parent" will load the specified location.
Image Image
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

FINALLY.

Code: Select all

<a href="javascript:self.close();window.opener.location.href='PARENT_REFRESH_LOCATION'">close this window</a>
Image Image
Post Reply