Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

win.focus - not reload the page?

Status
Not open for further replies.

nohandlesleft254

IS-IT--Management
Apr 19, 2006
58
GB
Hi,

I'm using the code below to check whether a quote has already been opened, and if so just focus it rather than open again. Only thing i cant work out how to do is get it to not reload the page on focus....?

<!-- OPEN QUOTE FUNCTION
<!-- First set the variable for the window name -->

function OpenQuote(theURL,winName,features)
{

var win = window.open(theURL,winName,features);

<!-- Then check if a window with that name exists -->
if (win)
{
<!-- If it does; focus that window -->
win.focus();
}
else
{
<!-- If it doesn't; create the new pop-up -->
var win = window.open(theURL,winName,features);
}
}
-->
 
Why not store win as a global variable rather than a local, and then simply test to see if undefined or not. This would save you having to open the window and close it again.

You should also test the "closed" property when doing this, as if the user has closed the window, you'll need to re-open it.

Hope this helps,
Dan

P.S. Try not to use HTML-style quotes in your script - they are invalid. You should really use "//" or "/* */" in JS for comments.

Apart from being invalid JS sytnax, I mention this because having HTML-style comments in CSS can cause CSS after them not to be evaluated properly, so I'm guessing the same goes for JS.

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan,

Thanks for your comments - i think ive understood what you meant but it is still reloading the page...?

// OPEN QUOTE FUNCTION

// First set the global variable for the window name
var win = window.open(theURL,winName,features);

function OpenQuote(theURL,winName,features)
{

// Then check if a window with that name exists
if (win && !win.closed)
{
// If it does; focus that window
win.focus();
}
else
{
// If it doesn't; create the new pop-up
var win = window.open(theURL,winName,features);
win.focus();
}
}
 
Ok, so its coming back 'undefined' because im setting the variable with '(theURL,winName,features)' as opposed to '(EditQuote.cfm?SerNr=1234,'1234','')' ...? I tried delcaring the global var inside the function (win = window.open(theURL,winName,features);)where those vars would have values but that doesnt seem to work either - what am i missunderstanding?

// OPEN QUOTE FUNCTION

// First set the variable for the window name
var win = window.open(theURL,winName,features);

function OpenQuote(theURL,winName,features)
{
alert (win)
// Then check if a window with that name exists
if (win && !win.closed)
{
// If it does; focus that window
win.focus();
}
else
{
// If it doesn't; create the new pop-up
var win = window.open(theURL,winName,features);
win.focus();
}

}
 
This is wrong:

Code:
var win = window.open(theURL,winName,features);

You are not passing any valid parameters there, only strings that have never been defined anywhere.

Try this instead (note: I've not test it):

Code:
war win;
function OpenQuote(theURL, winName, features) {
	if (win && !win.closed) {
		win.focus();
	} else {
		win = window.open(theURL, winName, features);
		win.focus();
	}
}

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Note: this solution will only ever work for one popup window. If you want each differently-named window to know whether they are already open or not, you should store the window handle in a hash table using the window name as the key (you can use an object / associative array for this purpose).

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Going back to the original point; this method still reloads the window when focused...
 
Hi Dan,

Sorry i havent replied - the last example you gave still refreshed the window...

i havent looked at it for a couple of days, but what i think im going to do is (as i have an action page prior to the page that displays the info and edit options) if the window is open ill send the user straight to the info page (meaning a reload doesnt matter) and if not send to action page.

An unneccessary compromise?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top