Refresh page with Javascript - javascript

I have a button on a page1 which opens a new page2 in the existing window. When page2 is closed i want page1 to be refreshed.
It should be said that page1 is not the parent of page2 even though the button is triggered from page1.

You can open the 2nd window using javascript and set an interval to see if it is closed. When it is closed you can then refresh your page.
// Code goes here
var childWindow;
var timer;
function openWindow() {
childWindow = window.open('http://google.com','','toolbar=0,status=0,width=626,height=436');
timer = setInterval(checkChild, 500);
}
function checkChild() {
if(childWindow && childWindow.closed) {
alert("Child window closed");
clearInterval(timer);
window.location.reload();
}
};

Try something like this, it should work
function buttonClick(url){
newWindow=window.open(url);
newWindow.onunload=function{ window.location.reload();};
}
Note: onunload event works only in IE and Firefox

Related

Scroll top after popping up a new window. - Jquery [duplicate]

This question already has answers here:
How to check if element is visible after scrolling?
(46 answers)
Closed 5 years ago.
So currently on button click I'm popping up a new window that goes to some website. But not only do I need to pop open to this new site but I also need to scroll down a little on the popped up screen. Here is what I'm currently doing:
$(document).ready(function () {
$('.popup').click(function(event) {
event.preventDefault();
window.open("https://www.spmet.aspx", "popupWindow", "width=1400,height=600,scrollbars=yes");
setTimeout(function() { scrollTo(0,150) }, 1000);
});
});
This currently opens the window successfully but how do I amend a change that will scroll to a particular spot in the site.
your code is runnning in the parent window, not in the pop up one. If you canĀ“t put int in the pop up target, try scrolling by reference adding "#btnLogin_div" top the url like this:
$(document).ready(function () {
$('.popup').click(function(event) {
event.preventDefault();
window.open
("https://www.spmet.aspx",
"popupWindow", "width=1400,height=600,scrollbars=yes");
setTimeout(function() { scrollTo(0,150) }, 1000);
});
});
Because You open a new window, js is loaded second time and click event wasn't executed. You can use something like:
$(document).ready(function () {
if (window.location.href.indexOf("my_url") > -1) {
$('html,body').animate({ scrollTop: "0px" }); // or 150px
}
});
or use some global variable for setTimeout and break it after execution.
Since you are opening a new window; the js on your old page is not used; you have to add the scroll js on the new page you are loading. This is good cause otherwise I could have you go to a page and phantom click on stuff, which would not be good.
You can get around it by opening the page in an iframe; where you can scroll your iframe window.

retain popup window when navigating website

The following code produces a javascript popup when a page is loaded and then closes the popup window when the parent window is closed.
<script language="JavaScript">
var player;
function load() {
player = window.open('http://www.google.com','player','height=320,width=320,scrollbars,resizable');
}
function unload() {
player.close();
}
window.onload = load;
window.onunload = unload;
</script>
This is working fine, however it closes the popup window when switching pages on the site. Is there any way this can be altered to keep the popup window until the actual URL/Site window is closed?
I think you have to check the status of the parent window from the child, instead of vice-versa.
This code has not been tested, but I'm taking a stab at the concept:
window.opener.onunload =
setTimeout(
function(){
if (!window.opener) self.close();
}, 1000);
Short answer is NO, the only way to hook into the event that closes the window is onunload, which fires when the document is unloaded (like when you navigate to another page).
Maybe you could check window.opener on the popup (with a timer, or onfocus), to check if the window that originated the popup still exists, then close the popup if it doesn't.
This may help you:
<script language="Javascript">
var player;
function load() {
player = window.open('http://www.google.com','player','height=320,width=320,scrollbars,resizable');
}
function unload() {
if (!(window.location.equals('http://yourSiteName.com'))) {
player.close();
}
}
window.onload = load();
window.onunload = unload();
</script>

Get Popup window After page change

Have a flash player that pops out into a separate popup browser window. And on the source page the flash player just displays a message that it is currently popped out.
Now if the user navigates away from the source page (to another page on the same domain) how do i get a reference to the popup or just detect if its open (using javascript on the new page)?
Figured this out. Had to have a javascript timer in the popped out window that trys to execute a function in the parent window that if exists will simply hide the player.
example
Popout window script
<script>
if(window.opener!=null)
setTimeout("popCheck();",2000);
function popCheck()
{
//Use try catch to prevent premission denied msgs in
//case parent window is on different domain
try
{
window.opener.setPoppedOut();
}
catch(e)
{
}
//Check Every 2 seconds
setTimeout("popCheck();",2000);
}
window.onbeforeunload = function()
{
//Used to facilitate popin on window close
if(window.opener!=null)
{
try
{
window.opener.setPoppedIn();
}
catch(e)
{
}
}
}
</script>
Parent window script
<script>
//Save the previous contents so that we can restore them later.
window.popoutContent = document.getElementById("popoutContent").innerHTML;
function setPoppedOut()
{
document.getElementById("popoutContent").innerHTML = "Player is Popped Out";
}
function setPoppedIn()
{
document.getElementById("popoutContent").innerHTML = window.popoutContent;
}
</script>

JavaScript Pop Up Issue

This happens in IE only (works in Chrome, Firefox):
The first time I click on the link, it opens the pop up, the second time, it just redirects me to the page.
When I refresh the page, the popup works the first time and then the second time, it again redirects.
function popUpWindow(win) {
myWindow = window.open(win, "popupwin", "height=310,width=570,resizeable=0, menubar=0,toolbar=0,location=0, directories=0,status=0");
}
This is how I call the function
User Status
EDIT: I apologize, the second time, it does not redirect, nothing happens when i click on the link It is redirecting.
EDIT: I close the popup when it opens up the first time and then click the popup link again.
I tried all the solutions presented below, what I found is that the JavaScript method is not called the second time I click on the link.
If the issue is that the popup is being redirected the second time, this is correct behavior according to the code. The name parameter is specified so that a popup can be reused. I don't necessarily recommend this, but you could use this to open multiple popups:
var popupIndex = 0;
function popUpWindow(win) {
myWindow = window.open(win, "popupwin" + popupIndex++, "height=310,width=570,resizeable=0, menubar=0,toolbar=0,location=0, directories=0,status=0");
}
Check whether the window has already been opened or been opened and subsequently closed. The following will focus the pop-up if it exists or open a pop-up otherwise. It's not clear whether you always want a new pop-up or whether focussing an existing one is good enough.
var myWindow = null;
function popUpWindow(win) {
if (!myWindow || myWindow.closed) {
myWindow = window.open(win, "popupwin", "height=310,width=570,resizeable=0, menubar=0,toolbar=0,location=0, directories=0,status=0");
} else {
myWindow.focus();
}
}
If you always want to close an existing pop-up before opening a new one:
var myWindow = null;
function popUpWindow(win) {
if (myWindow && !myWindow.closed) {
myWindow.close();
}
myWindow = window.open(win, "popupwin", "height=310,width=570,resizeable=0, menubar=0,toolbar=0,location=0, directories=0,status=0");
}
I assume you want to refresh the window which you would do like this:
var myWindow = null;
function popUpWindow(win) {
if (myWindow && !myWindow.closed) {
myWindow.close();
}
myWindow = window.open(win, name, options);
}

Auto refresh parent window after closing popup

I am having 2 pop up screens in my (Main) jsp.
In the first pop-up the user will update the required information(Update) and after submitting the info, a new pop up will be shown that shows the modifications(View).
I would like to refresh the Main page when the user clicks on the close "X" in the view page.
I tried to use some scripts like the following in the view page, but it did not work:
<script language="JavaScript">
function refreshParent() {
window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow)
{
window.opener.progressWindow.close()
}
window.close();
}
</script>
Try putting this javascript code in your popup window:
window.onunload = function(){
window.opener.location.reload();
};
function ref_and_close()
{
opener.location.reload();
self.close();
}
//just call the function
ref_and_close();

Categories