check window open - javascript

var windowUrl = "";
var windowName = "mywin";
var w = window.open(windowUrl, windowName, windowSize);
w.document.write(html);
w.document.close();
This is tied to onclick. I do not want to write or close or even open if window exists (maybe refocus instead). I found some examples, but they do not seem to "work" (or do what I think I need.)

Try the following:
<script type="text/javascript">
function loadUniquePage(page)
{
if (opener && !opener.closed) {
opener.focus();
} else {
var myWin = window.open(page,'','width=800,height=600');
opener = myWin;
}
}
</script>
also take a look at this question - JavaScript window.open only if the window does not already exist
also take a look on the link provided by #Aram Kocharyan:
https://developer.mozilla.org/en/DOM/window.closed

Related

check if the page print has been printed and then close it

I want to check if the page has been printed and then close it.
I searched a lot but there is no answer
I tried:
1- window.matchMedia('print') but it return always false.
2-window.onafterprint it doesn't work anymore.
3- I can not create a new doc and insert body of my page in it because my page comes from Crystelreport and it does have a barcode.
My code is simple:
var win = window.open(url, 'popup', 'toolbar = no, status = no, scrollbars = yes, resizable = yes');
win.onafterprint = function () {
win.close();
}
Try this:
var win = window.open(url, 'popup', 'toolbar = no, status = no, scrollbars = yes, resizable = yes');
win.onafterprint = function (e) {
e.target.close();
}

How to get Chrome to throw exception when changing url if it fails

I have a reference to a new window opened with js
var theNewTab="";
theNewTab = window.open(theURL, 'winRef');
then I change the url in the as the user clicks on another link in the parent window using
theNewTab.location.href = targetLink;
theNewTab.focus();
The problem i'm having with chrome that id doesn't throw exception if the the window doesn't exist anymore "closed" unlink FF & IE which im using to open the window again.
try {
theNewTab.location.href = targetLink;
theNewTab.focus();
}catch(err) {
theNewTab = window.open(theURL, 'winRef');
theNewTab.focus();
}
PS: I tried to use "window.open" every time but if the window already open, id does not reload the page or it does but it doesn't re-execute the script I have in document ready I think.
I'm not sure what you need.
<script type="text/javascript">
var theNewTab = null;
function openNewTab(theURL) {
if (theNewTab == null || theNewTab.closed == true) {
theNewTab = window.open(theURL);
} else {
theNewTab.location.href = theURL;
}
theNewTab.focus();
};
// use the function when you need it
$('a').click(function() {
openNewTab($(this).attr('href'));
});
</script>
Is this example helpful for you?

How to set value of child pop op elements from parent window using Javascript?

I am calling a pop up window from a parent page using :
var childWindow = open('test1.aspx', '1397127848655', 'resizable=no,width=700,height=500');
I then try to set the value of two spans which are on chil pop up from parent window using this childWindow object.
childWindow.onload = function () {
alert('this msg does not shows up when run on IE8');
var hidden1 = childWindow.document.getElementById('hidden1');
var hidden2 = childWindow.document.getElementById('hidden2');
hidden1.innerHTML = rowindex;
hidden2.innerHTML = controlname;
};
this code works fine as long as I am using chrome. But it refuses to work on IE8. There are no console errors either.
I tried removing childWindow.onload = function () { } but then the page would just sort of refresh on both chrome and IE8.
UPDATE
This did not work either.
function CallPopUp(rowindex,controlname ) {
function popupLoad() {
alert('this msg does not shows up when run on IE8');
var hidden1 = childWindow.document.getElementById('hidden1');
var hidden2 = childWindow.document.getElementById('hidden2');
hidden1.innerHTML = rowindex;
hidden2.innerHTML = controlname;
}
var childWindow = open('test1.aspx', '1397127848655', 'resizable=no,width=700,height=500');
if (childWindow.document.readyState === "complete") {
popupLoad();
} else {
childWindow.onload = popupLoad;
}
If test.aspx is in the browser cache, it is possible that the onload event has already happened before you attach your event handler so you're missing it (IE is known to do this with image load events). I'd suggest you check document.readyState before attaching your event handler.
function popupLoad() {
alert('this msg does not shows up when run on IE8');
var hidden1 = childWindow.document.getElementById('hidden1');
var hidden2 = childWindow.document.getElementById('hidden2');
hidden1.innerHTML = rowindex;
hidden2.innerHTML = controlname;
}
var childWindow = open('test1.aspx', '1397127848655', 'resizable=no,width=700,height=500');
if (childWindow.document.readyState === "complete") {
popupLoad();
} else {
childWindow.onload = popupLoad;
}
As another option, you can put the values into the query parameters for the URL:
`"test1.aspx?hidden1=" + rowindex + "&hidden2=" + controlname`
and then have the popup window load it's own fields from it's own onload handler from what's in the query string. Then, you can keep the code in the popup window self contained and you don't have to try to modify one window from another.
If you don't want the user to see this or be able to edit it, you can turn off the location bar in the popup window.

window.self.close() not working

I opened a print window using window.print(). I tried using window.self.close(),
but I was unable to close that one. I am using Firefox. My idea was to close the window by itself if the user doesn't perform any action on it.
This is the code I am using for print window.
$('.click-print-paybymail').live("click", function (e) {
var amount = $('.amount-enclosed').val();
var ccnum = $('.credit-card-account-number').val();
var isAllow = true;
if (!isValidCC(ccnum)) {
isAllow = false;
}
if (!isValidAmount(amount)) {
isAllow = false;
}
if (isAllow) {
window.print();
}
});
You can not close the print dialog programmatically from javascript since it is not a browser window - its an operating system dialog.

Putting HTML from the current page into a new window

I want to open a new window and carry over some of the HTML in the original page to the new window. What is the simplest way to do this?
Something like:
$("div#foo").click( function(){
var copyHTML = $("table.bar").html();
window.open(''); // somehow put copyHTML in the new window
});
Try the following:
$("div#foo").click
(
function()
{
var copyHTML = $("table.bar").html();
var newWindow = window.open('');
newWindow.document.body.innerHTML = copyHTML;
}
);
This will work in some cases, and is the easier than the next approach.
If you get security warnings from your browser, the next approach may be more agreeable. Add a function within the parent page called getContent, like so:
function getContent()
{
return $("table.bar").html();
}
...and on document.ready in the child window do the following:
$(document).ready
(
function()
{
var parentContent = window.opener.getContent();
$("body").html(parentContent);
}
);

Categories