Thursday, October 11, 2007

Change Url of Popup without closing it

On one of my project, I encountered a problem.I have 2 buttons on the page and on click of any button i have to open a pop up window.
Let's say on click of button 1 -> pop up window should open with Page1.aspx
and on click of button 2 -> pop up window should open with Page2.aspx.

But the problem was I have to use only one instance of pop up window.One way is I can close the existing pop up window and open another will other Url.But this is not I want.

I want to use the same window and if it is open just change the Url.

After some googling I Found a solution:-

var childRef = null;

function openpopup(page)
{
if (childRef == null childRef.closed)
{
childRef = window.open(page1 , '');
}
else
{
childRef.location.href=page;
childRef.focus();
}
return false;
}

This Java Script function was called onClientClick event of the button and respective page value was passed as parameter.

ChildWindowObject.Location.href = Some Url does the trick for me.It will change the Url of popup.

One thing to notice here..

If I dont use childRef.focus(); this code then what is going to happen If the pop up window is open (not Minimized) and when childRef.location.href=page; this gets excuted pop up window will be minimized automatically.

To keep it not minimized I used childRef.focus();. This will set the focus again on the window.

Enjoy..

Happy Programming...

2 comments:

Chirag said...

Hello Virendra,

This can be done if you pass popup name same to all the links.

for eg:

window.open('http://www.yahoo.com','new','height=400,width=500')


window.open('http://www.hotmail.com','new','height=400,width=500')

Check in window.open function of javascript 2nd para. is the popup name, if we pass same then it will change url automatically

Regards,
Chirag

Virendra said...

Hi,
I have not checked this.
But if it works then it will save lots of lines of code.

Let me check this...

Thanks..

back to top