Open child window and redirect parent window - javascript

I have a JavaScript that opens a new window when a button is clicked. It works fine. But, I want to re-direct the Parent Window (i.e. the current page) once the new window is opened.
Here is my script :
<script type="text/javascript">
function OpenWindow() {
window.open('/My_Folder/file.php',
'newwindow',
config='height=670,width=1400,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no');
}
</script>
And, the button:
<input type="button" value="Submit" onclick="OpenWindow()">
So, in order to be able to re-direct the Parent Window, after the new window has popped up, I added a simple line to my JS function :
<script type="text/javascript">
function OpenWindow() {
window.open('/My_Folder/new_file.php', 'newwindow',
config='height=670,width=1400,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no, status=no');
}
setTimeout(1000, '/My_Folder/PHP/file_2.php');
</script>
Meaning, after the new window has popped up, the Parent Window should re-direct in 1 second.
But, for some reason, it's not working.
Online searches have only given me solutions for situations involving the reverse : re-direct the Parent Window after CLOSING the child-window.
I want to re-direct the Parent Window after opening a new (child) window.
UPDATE
So far, I've tried these 3 ways :
(a) <script type="text/javascript">
function OpenWindow() {
window.open ('/My_Folder/new_file.php', 'newwindow',
config='height=670, width=1400,
toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no,
directories=no, status=no');
window.top.location.href = '/My_Folder/PHP/file_2.php'
}
</script>
(b) <script type="text/javascript">
function OpenWindow() { window.open('/My_Folder/file.php','newwindow',
config='height=670,width=1400,toolbar=no,menubar=no,scrollbars=no,
resizable=no, location=no,directories=no,status=no');
settimeout( function(){document.location='/My_Folder/PHP/file_2.php'}, 1000
);
}
</script>
(c) <script>
function OpenWindow() {
window.open ('/My_Folder/new_file.php', 'newwindow', 'height=670,
width=1400, toolbar=no, menubar=no, scrollbars=no, resizable=no,
location=no, directories=no, status=no');
window.location = '/My_Folder/PHP/file_2.php';
}
</script>
Of all these, none are working.
But (b) is the best solution so far, because, at least, it opens the New (Child) Window, as it should. But, it still does not re-direct the Parent Window :(((

Try this:
<script type="text/javascript">
function OpenWindow() { window.open('/My_Folder/file.php','newwindow',
config='height=670,width=1400,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no');
settimeout( function(){document.location.href='/My_Folder/PHP/file_2.php'}, 1000 );
}
</script>
Moves the settimeout into the function and uses it properly, and tells the window to change to a new location.

My javasrcipt isnt too hot, but does this not work ?
<script type="text/javascript">
function OpenWindow() {
window.open ('/My_Folder/new_file.php', 'newwindow',
config='height=670, width=1400,
toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no,
directories=no, status=no');
window.top.location.href = '/My_Folder/PHP/file_2.php'
}
</script>

Related

How to close printing window

I want to close the window which has printing dialog on that using window.close() function.
I realize that, if the window does not have printing dialog, the window.close() function will works fine.
Here is the example code:
<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
}
function closeWin() {
myWindow.close();
}
</script>
But if I put the print() function into the above code, I will not be albe to click the close window button until the printing dialog is closed.
Here is the example code
<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.print();
}
myWindow.print() {
myWindow.close();
}
I want to know why does that happen and how can I close the window which contains printing dialog.
Thank you.
Instead of doing mywindow.print() myWindow.close(); separately to the function, put myWindow.close() inside the function then just call that. It seems weird that you're doing myWindow.print() and closing inside of that, I dont think that'd work.

Close Child Window with Parent Window After reload

Right now, I can close child popup on parent page close. But in child popup, I make a function to reload parent page. After reload parent page I cannot close child popup on parent page close. Because the popup information is missing. Anyone suggest a solution ?
I have try to store win.open to sessionStorage, but sessionStorage can only store String information.
Here my code:
winOpen.js
var win;
$(function() {
$(".closeBtn").click(function() {
win.close();
});
});
function winOpenCenterParseReferer(add){
var url = add;
win = window.open(
"", ""
, "width=1200, height=600, location=no, menubar=no, toolbar=no, status=no, scrollbars=yes, resizable=no");
win.location.href = url;
};
parent.html
<html>
<head>
<script type="text/javascript" src="winOpen.js"></script>
</head>
<body>
close
Open Popup
</body>
</html>
popup.html
<html>
<head>
</head>
<body>
Reload Parent
</body>
</html>

How to disable the browser parent window when child window open?

I have a scenario like when i open the child widow by clicking the button in the parent window, here i want to disable the entire parent window.
I have seen many examples in the stackoverflow and google but i had not any luck and i used the following code and it does not work for me
<script>
var childWin = window.open(url, "_blank", "enabled");
childWin.focus();
</script>
try this
<html>
<head>
<script type="text/javascript">
var popupWindow = null;
function child_open() {
popupWindow = window.open('URL', "_blank", "directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200");
}
function parent_disable() {
if (popupWindow && !popupWindow.closed)
popupWindow.focus();
}
</script>
</head>
<body style="width: 670px; height: 670px" onfocus="parent_disable();" onclick="parent_disable();">
Click me
</body>
</html>
Hope this helps....

win.onload not working

This is my code.My prblem is that when i click on button the window.open function work properly and it show a popup window but after this,page cannot redirect to define location:
<script type="text/javascript">
var win=null;
$(document).ready(function() {
$("#buttonid").live("click",function(){
var alt = "http://www.testhost.com/test.php";
var rel = "http://www.testhost.com/test2.php";
var width= (window.innerWidth)-450;
var win = window.open(alt,"mywin","width=450,height="+window.innerHeight+",left="+width+", location=no, menubar=no, status=no, titlebar=no, scrollbars=no");
win.onload=function(){
window.location=rel;
}
win.blur();
//setTimeout(win.focus(), 0);
return false;
});
});
</script>
</head>
<body>
<button id="buttonid">Click</button>
</body>
You need to specify it as below,
win.onload=function()
{
window.open.location=rel;
}
Take a look at this post: Detecting the onload event of a window opened with window.open
If you are opening a window to a different domain, you will not be able to tie into this event.
That post has several things you can try.
win.addEventListener('load', function () {
alert('popup loaded');
}, false);
This is a more ideal cross-browser solution (again, it's from that link):
win[win.addEventListener ? 'addEventListener' : 'attachEvent'](
(win.attachEvent ? 'on' : '') + 'load', function () {
alert('popup loaded');
}, false
);
EDIT: Just tried the second code snippet in IE10, FF18, and Chrome 25. The onload event worked great, as long as it was pointing to a URL that was on the same domain as the site calling it. Even I pointed the URL to www.google.com, my event never fired. This is a security feature of the browsers, and I don't think you'll be able to do much about it...

Refreshing Parent window after closing popup

I am creating a popup window that goes to hello.html. I want my original (parent page) to reload when i close the popup window (hello.html). I can't seem to get it to work, but I'm close. Here is the code I have so far for the main page and the hello.html page....
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function open_win()
{
window.open("hello.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
}
</script>
<script language="JavaScript">
function refreshParent() {
window.opener.location.href = window.opener.location.href;
if (window.opener.hello.html)
{
window.opener.hello.html.close()
}
window.close();
}
</script>
</head>
<body>
<script type="text/javascript">
var d=new Date();
document.write(d);
</script>
<form>
<input type="button" value="Open Window" onclick="open_win()">
</form>
</body>
</html>
Here is the hello.html...
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
</script>
</head>
<body>
Hello
</body>
</html>
Subscribe to the unload event in the child window and call the parent window from the child window to notify it is closing!
Edit Added a code sample...
function popupClosing() {
alert('About to refresh');
window.location.href = window.location.href;
}
var w = window.open("hello.html","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
w.onunload = function () {
window.parent.popupClosing()
};
Do it like this, after creating the popup monitor its "closed" status property in an interval. But this is added in the parent document:
var pop = window.open("page.html", "popup",
"width=800,height=500,scrollbars=0,title='popup'");
pop.focus();
var monitor = setInterval(function() {
if (pop.closed) {
document.location.reaload()
}
}, 1000);
Try putting this javascript code in your popup window:
window.onunload = function(){
window.opener.location.reload();
};
/onunload event will trigger when you close your popup window, and window.opener.location.reload() will reload the source (parent) window./
on click event of popup close function Try...
window.opener.location.reload(true);
This piece of code works as well if a new window is popped with the window.open feature.
setTimeout(function () { window.opener.location.reload(true); window.close();}, 3000);
Put this script in the header of your popup window:
<script type='text/javascript'>
window.onunload = function(){
window.opener.location.reload();}
</script>
This will reload the parent window when you close the popup.

Categories

Resources