Javascript question: How do I refresh a window remotely without it being triggered by an event? Ie. I don't want an Onlick, I just want a window.refresh SCRIPT>
September 27, 2005 3:01 AM   Subscribe

Javascript question: How do I refresh a window remotely without it being triggered by an event? Ie. I don't want an Onlick, I just want a window.refresh SCRIPT>

I know I'm on the right lines with window.refresh(); but how do I specify a target?

Thanks so much in advance! I do know a bit of javascript but no enough to choose correct google terminology.

I'm trying to write a php script in a remote window that writes to a database then refreshes the main window and closes itself (i have the code for this)
cheers.
posted by jwhittlestone to Computers & Internet (8 answers total)
 
In your calling window (the main window), include the following function:
function change(href)
{
	window.location.href = href;
}
In your remote window, you can simply call
opener.change('http://www.google.com');
and voilĂ ! Remote window changin' goodness. :)
posted by PuGZ at 3:38 AM on September 27, 2005


Which window is the main window and which window is the target (e.g. is it a frame or a popup)?

The proper command to refresh a page is location.reload() or you can use location.reload(true) to force a reload even if nothing was changed. All you need to do is assign it to a target (e.g. in those two previous examples it would reload the window it was run from). To assign it to a popup you would need a handle to a popup, here's how:


//open a new window... btw popups suck
var handler = open("foo.html", ...);

//refresh popup
handler.location.reload(true);


Now, to force refresh of the popup on a regular basis, you'd need to use the setInterval function. For example, if your popup is generated onload (I'm going to hell I know) then you might use code similar to this:



var popupHandler; // handler has global scope, necessary
var intervalHandler; //handler for refresh so you can stop it if you want

//function to refresh the popup, duh.

function refreshPopup() {

//you probably want validation or a try catch here in case of popup blockers
popupHandler.location.reload(true);
}

//anonymous function that triggers when the main window finishes loading. Anything in this function will be executed.

onload = function () {

//open a popup
handler = open("foo.html", .....);

//set popup to refresh once every 15 seconds
intervalHandler = setInterval("refreshPopup",15000);
}

The same can be done with frames by appending location.reload() to the end of a reference to the appropriate frame. I'd give you an example but frames suck even more than popups and so I've forgotten the syntax. It's something to the effect of top.document.frames["frameName"].location.reload(). Hope that gets you in the right direction.
posted by furtive at 4:06 AM on September 27, 2005


Oh, I just reread your question, what you want is this:


onload = function () {
opener.location.reload();
window.close();
}

posted by furtive at 4:17 AM on September 27, 2005


Response by poster: Urm. Ok, still a bit stumped (me=a little bit simple)

I have the main window displaying a picture and a button that gives the option for the user to change the pic

On clicking the button, a javacript function is called to open a new window that lets the user browse for the picture and upload it to the database upon submitting.

Upon submitting in the new window, i want the main page to refresh showing the updated picture.

is this possible? cheers
Jon
posted by jwhittlestone at 5:01 AM on September 27, 2005


I'm assuming you already have the popup code and picture submission code in place. For the purpose of this example, let's assume your upload form submits the image to action.php

In action.php you should include something akin to the following:
echo '<body onLoad="opener.location.reload();window.close();">';
Of course you will probably already have an existing <body> tag, which you would have to modify accordingly. Hope that helps!
posted by PuGZ at 5:10 AM on September 27, 2005


Response by poster: Thanks so much, im nearly there i think!

ive included this function in my javascript file

function refreshopener() {
opener.location.reload();
window.close();
}

then, i've done this in the html body popup. like i say, i don't want it on an event, just to happen once the server side scripting has done the database funcitons

refreshopener();

It doesn't seem to work, surely im on the right track?
posted by jwhittlestone at 5:12 AM on September 27, 2005


well, you can't refresh the main window until the submit has been completed, which rules out doing a location.reload() in the onsubmit even handler, so you have to do it on the onload of the popup when it reloads (assuming it submits to itself). Basically, you said you already have a close() to get rid of the popup, so you'll want the opener.location.reload() right before that close().

IRC would be a much better venue for discussing things like this. There are excellent #javascript channels on the undernet and freenode.
posted by furtive at 5:13 AM on September 27, 2005


Response by poster: Basically "opener.location.reload();" was the statement i was after and i now know that.

thanks to both of you, a real help - i'll try it when i get home as i seem to have buggered up my PHP script here!

thanks again.
posted by jwhittlestone at 5:34 AM on September 27, 2005


« Older To mate or not to roomate   |   Video all in lphabet letters. Newer »
This thread is closed to new comments.