Event listeners are not working on popup window when close it's parent windowRunning Javascript in new window.openInject an opened window with scriptWhy don't self-closing script elements work?How to find event listeners on a DOM node when debugging or from the JavaScript code?About Popup window data to Parent windowRefresh parent window after closing a popup windowJavascript: Close Popup, Open New Window & Redirecthow to close a popup window when the focus is gone out from itCallback for a popup window in JavaScriptPopup window losing focusCannot display HTML stringI want to open a popup window on a click and insert a button in the new window to allow closing by user

Picking a theme as a discovery writer

Is it safe to keep the GPU on 100% utilization for a very long time?

What’s the interaction between darkvision and the Eagle Aspect of the beast, if you have Darkvision past 100 feet?

Did Ham the Chimp follow commands, or did he just randomly push levers?

shebang or not shebang

Displaying an Estimated Execution Plan generates CXPACKET, PAGELATCH_SH, and LATCH_EX [ACCESS_METHODS_DATASET_PARENT] waits

How do I give a darkroom course without negs from the attendees?

Latex editor/compiler for Windows and Powerpoint

Would a legitimized Baratheon have the best claim for the Iron Throne?

Did any early RISC OS precursor run on the BBC Micro?

In a series of books, what happens after the coming of age?

What is the meaning of "matter" in physics?

Why doesn't increasing the temperature of something like wood or paper set them on fire?

And now you see it

Appropriate age to involve kids in life changing decisions

Splitting polygons and dividing attribute value proportionally using ArcGIS Pro?

Can you just subtract the challenge rating of friendly NPCs?

What calendar would the Saturn nation use?

Select list elements based on other list

Convert a huge txt-file into a dataset

What is more safe for browsing the web: PC or smartphone?

While drilling into kitchen wall, hit a wire - any advice?

What is the Ancient One's mistake?

If quadruped mammals evolve to become bipedal will their breast or nipple change position?



Event listeners are not working on popup window when close it's parent window


Running Javascript in new window.openInject an opened window with scriptWhy don't self-closing script elements work?How to find event listeners on a DOM node when debugging or from the JavaScript code?About Popup window data to Parent windowRefresh parent window after closing a popup windowJavascript: Close Popup, Open New Window & Redirecthow to close a popup window when the focus is gone out from itCallback for a popup window in JavaScriptPopup window losing focusCannot display HTML stringI want to open a popup window on a click and insert a button in the new window to allow closing by user






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I want to make my print button working on the popup window but the problem is when I close the parent window or navigate on another URL in the parent window then print button from my popup window is not working.



Event listeners are not working in the popup window may be they disappear with the parent window.



Note: It's working fine when the parent window is there



Below is the code which is I am using please help me out



<!DOCTYPE html>
<html>
<body>

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
var myWindow = window.open("", "_blank", "width=200,height=100");
myWindow.document.write("<p>A new window!</p>");
var printButton = myWindow.document.createElement('BUTTON');
var buttonNode = myWindow.document.createTextNode('PRINT');
printButton.appendChild(buttonNode);
myWindow.document.body.insertAdjacentHTML('beforeend', printButton.outerHTML);
var btn = myWindow.document.querySelector('button');
btn.addEventListener('click', () =>
myWindow.window.print();
);

</script>

</body>
</html>









share|improve this question






















  • stackoverflow.com/questions/32357312/…, stackoverflow.com/questions/29909927/…, OR jsfiddle.net/murx2o9p .. I don't like my own solution though.

    – Jason
    Mar 23 at 6:36












  • Works for me in Safari

    – Andrii Rudavko
    Mar 23 at 9:49











  • @Jason also I don't like your solution.....but thanks for the response

    – Krishna Ghatul
    Mar 27 at 6:54


















0















I want to make my print button working on the popup window but the problem is when I close the parent window or navigate on another URL in the parent window then print button from my popup window is not working.



Event listeners are not working in the popup window may be they disappear with the parent window.



Note: It's working fine when the parent window is there



Below is the code which is I am using please help me out



<!DOCTYPE html>
<html>
<body>

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
var myWindow = window.open("", "_blank", "width=200,height=100");
myWindow.document.write("<p>A new window!</p>");
var printButton = myWindow.document.createElement('BUTTON');
var buttonNode = myWindow.document.createTextNode('PRINT');
printButton.appendChild(buttonNode);
myWindow.document.body.insertAdjacentHTML('beforeend', printButton.outerHTML);
var btn = myWindow.document.querySelector('button');
btn.addEventListener('click', () =>
myWindow.window.print();
);

</script>

</body>
</html>









share|improve this question






















  • stackoverflow.com/questions/32357312/…, stackoverflow.com/questions/29909927/…, OR jsfiddle.net/murx2o9p .. I don't like my own solution though.

    – Jason
    Mar 23 at 6:36












  • Works for me in Safari

    – Andrii Rudavko
    Mar 23 at 9:49











  • @Jason also I don't like your solution.....but thanks for the response

    – Krishna Ghatul
    Mar 27 at 6:54














0












0








0








I want to make my print button working on the popup window but the problem is when I close the parent window or navigate on another URL in the parent window then print button from my popup window is not working.



Event listeners are not working in the popup window may be they disappear with the parent window.



Note: It's working fine when the parent window is there



Below is the code which is I am using please help me out



<!DOCTYPE html>
<html>
<body>

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
var myWindow = window.open("", "_blank", "width=200,height=100");
myWindow.document.write("<p>A new window!</p>");
var printButton = myWindow.document.createElement('BUTTON');
var buttonNode = myWindow.document.createTextNode('PRINT');
printButton.appendChild(buttonNode);
myWindow.document.body.insertAdjacentHTML('beforeend', printButton.outerHTML);
var btn = myWindow.document.querySelector('button');
btn.addEventListener('click', () =>
myWindow.window.print();
);

</script>

</body>
</html>









share|improve this question














I want to make my print button working on the popup window but the problem is when I close the parent window or navigate on another URL in the parent window then print button from my popup window is not working.



Event listeners are not working in the popup window may be they disappear with the parent window.



Note: It's working fine when the parent window is there



Below is the code which is I am using please help me out



<!DOCTYPE html>
<html>
<body>

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
var myWindow = window.open("", "_blank", "width=200,height=100");
myWindow.document.write("<p>A new window!</p>");
var printButton = myWindow.document.createElement('BUTTON');
var buttonNode = myWindow.document.createTextNode('PRINT');
printButton.appendChild(buttonNode);
myWindow.document.body.insertAdjacentHTML('beforeend', printButton.outerHTML);
var btn = myWindow.document.querySelector('button');
btn.addEventListener('click', () =>
myWindow.window.print();
);

</script>

</body>
</html>






javascript html html5 popup window






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 6:12









Krishna GhatulKrishna Ghatul

5810




5810












  • stackoverflow.com/questions/32357312/…, stackoverflow.com/questions/29909927/…, OR jsfiddle.net/murx2o9p .. I don't like my own solution though.

    – Jason
    Mar 23 at 6:36












  • Works for me in Safari

    – Andrii Rudavko
    Mar 23 at 9:49











  • @Jason also I don't like your solution.....but thanks for the response

    – Krishna Ghatul
    Mar 27 at 6:54


















  • stackoverflow.com/questions/32357312/…, stackoverflow.com/questions/29909927/…, OR jsfiddle.net/murx2o9p .. I don't like my own solution though.

    – Jason
    Mar 23 at 6:36












  • Works for me in Safari

    – Andrii Rudavko
    Mar 23 at 9:49











  • @Jason also I don't like your solution.....but thanks for the response

    – Krishna Ghatul
    Mar 27 at 6:54

















stackoverflow.com/questions/32357312/…, stackoverflow.com/questions/29909927/…, OR jsfiddle.net/murx2o9p .. I don't like my own solution though.

– Jason
Mar 23 at 6:36






stackoverflow.com/questions/32357312/…, stackoverflow.com/questions/29909927/…, OR jsfiddle.net/murx2o9p .. I don't like my own solution though.

– Jason
Mar 23 at 6:36














Works for me in Safari

– Andrii Rudavko
Mar 23 at 9:49





Works for me in Safari

– Andrii Rudavko
Mar 23 at 9:49













@Jason also I don't like your solution.....but thanks for the response

– Krishna Ghatul
Mar 27 at 6:54






@Jason also I don't like your solution.....but thanks for the response

– Krishna Ghatul
Mar 27 at 6:54













1 Answer
1






active

oldest

votes


















1














Instead of using an addEventListener rather use the onclick attribute so that it's persistent.



Demo: https://codepen.io/craigiswayne/pen/moYYga






function myFunction() 
var myWindow = window.open("", "_blank", "width=500,height=500");
myWindow.document.write("<p>A new window!</p>");

var printButton = myWindow.document.createElement('BUTTON');
printButton.innerHTML = "PRINT";
printButton.setAttribute("onclick", "window.print()");
myWindow.document.body.appendChild(printButton);

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>





The reason your way didn't work was because there was no reference to the original event handler once the parent changed.



The above code will not work on StackOverflow due to the sandboxed environment of the iframe that runs the code. So i've added the codepen link to demonstrate the solution






share|improve this answer























  • above solution is working fine for me .....thanks

    – Krishna Ghatul
    Mar 28 at 6:40












  • Hey, What if I don't want to include that print button on the print page? seems above code will not work for that scenario!

    – Krishna Ghatul
    Apr 1 at 5:41












  • What do you mean? Please explain a little futher

    – Craig Wayne
    Apr 1 at 5:42











  • When I am clicking on the print button it shows me a print window having a print button on it so i don't want that print button on the page(on print page)

    – Krishna Ghatul
    Apr 1 at 5:44











  • So you just want a new window no button? I'd suggest creating a new question with a description of what you're asking krishna

    – Craig Wayne
    Apr 1 at 5:46











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311129%2fevent-listeners-are-not-working-on-popup-window-when-close-its-parent-window%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Instead of using an addEventListener rather use the onclick attribute so that it's persistent.



Demo: https://codepen.io/craigiswayne/pen/moYYga






function myFunction() 
var myWindow = window.open("", "_blank", "width=500,height=500");
myWindow.document.write("<p>A new window!</p>");

var printButton = myWindow.document.createElement('BUTTON');
printButton.innerHTML = "PRINT";
printButton.setAttribute("onclick", "window.print()");
myWindow.document.body.appendChild(printButton);

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>





The reason your way didn't work was because there was no reference to the original event handler once the parent changed.



The above code will not work on StackOverflow due to the sandboxed environment of the iframe that runs the code. So i've added the codepen link to demonstrate the solution






share|improve this answer























  • above solution is working fine for me .....thanks

    – Krishna Ghatul
    Mar 28 at 6:40












  • Hey, What if I don't want to include that print button on the print page? seems above code will not work for that scenario!

    – Krishna Ghatul
    Apr 1 at 5:41












  • What do you mean? Please explain a little futher

    – Craig Wayne
    Apr 1 at 5:42











  • When I am clicking on the print button it shows me a print window having a print button on it so i don't want that print button on the page(on print page)

    – Krishna Ghatul
    Apr 1 at 5:44











  • So you just want a new window no button? I'd suggest creating a new question with a description of what you're asking krishna

    – Craig Wayne
    Apr 1 at 5:46















1














Instead of using an addEventListener rather use the onclick attribute so that it's persistent.



Demo: https://codepen.io/craigiswayne/pen/moYYga






function myFunction() 
var myWindow = window.open("", "_blank", "width=500,height=500");
myWindow.document.write("<p>A new window!</p>");

var printButton = myWindow.document.createElement('BUTTON');
printButton.innerHTML = "PRINT";
printButton.setAttribute("onclick", "window.print()");
myWindow.document.body.appendChild(printButton);

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>





The reason your way didn't work was because there was no reference to the original event handler once the parent changed.



The above code will not work on StackOverflow due to the sandboxed environment of the iframe that runs the code. So i've added the codepen link to demonstrate the solution






share|improve this answer























  • above solution is working fine for me .....thanks

    – Krishna Ghatul
    Mar 28 at 6:40












  • Hey, What if I don't want to include that print button on the print page? seems above code will not work for that scenario!

    – Krishna Ghatul
    Apr 1 at 5:41












  • What do you mean? Please explain a little futher

    – Craig Wayne
    Apr 1 at 5:42











  • When I am clicking on the print button it shows me a print window having a print button on it so i don't want that print button on the page(on print page)

    – Krishna Ghatul
    Apr 1 at 5:44











  • So you just want a new window no button? I'd suggest creating a new question with a description of what you're asking krishna

    – Craig Wayne
    Apr 1 at 5:46













1












1








1







Instead of using an addEventListener rather use the onclick attribute so that it's persistent.



Demo: https://codepen.io/craigiswayne/pen/moYYga






function myFunction() 
var myWindow = window.open("", "_blank", "width=500,height=500");
myWindow.document.write("<p>A new window!</p>");

var printButton = myWindow.document.createElement('BUTTON');
printButton.innerHTML = "PRINT";
printButton.setAttribute("onclick", "window.print()");
myWindow.document.body.appendChild(printButton);

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>





The reason your way didn't work was because there was no reference to the original event handler once the parent changed.



The above code will not work on StackOverflow due to the sandboxed environment of the iframe that runs the code. So i've added the codepen link to demonstrate the solution






share|improve this answer













Instead of using an addEventListener rather use the onclick attribute so that it's persistent.



Demo: https://codepen.io/craigiswayne/pen/moYYga






function myFunction() 
var myWindow = window.open("", "_blank", "width=500,height=500");
myWindow.document.write("<p>A new window!</p>");

var printButton = myWindow.document.createElement('BUTTON');
printButton.innerHTML = "PRINT";
printButton.setAttribute("onclick", "window.print()");
myWindow.document.body.appendChild(printButton);

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>





The reason your way didn't work was because there was no reference to the original event handler once the parent changed.



The above code will not work on StackOverflow due to the sandboxed environment of the iframe that runs the code. So i've added the codepen link to demonstrate the solution






function myFunction() 
var myWindow = window.open("", "_blank", "width=500,height=500");
myWindow.document.write("<p>A new window!</p>");

var printButton = myWindow.document.createElement('BUTTON');
printButton.innerHTML = "PRINT";
printButton.setAttribute("onclick", "window.print()");
myWindow.document.body.appendChild(printButton);

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>





function myFunction() 
var myWindow = window.open("", "_blank", "width=500,height=500");
myWindow.document.write("<p>A new window!</p>");

var printButton = myWindow.document.createElement('BUTTON');
printButton.innerHTML = "PRINT";
printButton.setAttribute("onclick", "window.print()");
myWindow.document.body.appendChild(printButton);

<p>Click the button to open a new window, and assure that the new window GETS focus (send the new window to the front).</p>

<button onclick="myFunction()">Try it</button>






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 11:57









Craig WayneCraig Wayne

2,39732235




2,39732235












  • above solution is working fine for me .....thanks

    – Krishna Ghatul
    Mar 28 at 6:40












  • Hey, What if I don't want to include that print button on the print page? seems above code will not work for that scenario!

    – Krishna Ghatul
    Apr 1 at 5:41












  • What do you mean? Please explain a little futher

    – Craig Wayne
    Apr 1 at 5:42











  • When I am clicking on the print button it shows me a print window having a print button on it so i don't want that print button on the page(on print page)

    – Krishna Ghatul
    Apr 1 at 5:44











  • So you just want a new window no button? I'd suggest creating a new question with a description of what you're asking krishna

    – Craig Wayne
    Apr 1 at 5:46

















  • above solution is working fine for me .....thanks

    – Krishna Ghatul
    Mar 28 at 6:40












  • Hey, What if I don't want to include that print button on the print page? seems above code will not work for that scenario!

    – Krishna Ghatul
    Apr 1 at 5:41












  • What do you mean? Please explain a little futher

    – Craig Wayne
    Apr 1 at 5:42











  • When I am clicking on the print button it shows me a print window having a print button on it so i don't want that print button on the page(on print page)

    – Krishna Ghatul
    Apr 1 at 5:44











  • So you just want a new window no button? I'd suggest creating a new question with a description of what you're asking krishna

    – Craig Wayne
    Apr 1 at 5:46
















above solution is working fine for me .....thanks

– Krishna Ghatul
Mar 28 at 6:40






above solution is working fine for me .....thanks

– Krishna Ghatul
Mar 28 at 6:40














Hey, What if I don't want to include that print button on the print page? seems above code will not work for that scenario!

– Krishna Ghatul
Apr 1 at 5:41






Hey, What if I don't want to include that print button on the print page? seems above code will not work for that scenario!

– Krishna Ghatul
Apr 1 at 5:41














What do you mean? Please explain a little futher

– Craig Wayne
Apr 1 at 5:42





What do you mean? Please explain a little futher

– Craig Wayne
Apr 1 at 5:42













When I am clicking on the print button it shows me a print window having a print button on it so i don't want that print button on the page(on print page)

– Krishna Ghatul
Apr 1 at 5:44





When I am clicking on the print button it shows me a print window having a print button on it so i don't want that print button on the page(on print page)

– Krishna Ghatul
Apr 1 at 5:44













So you just want a new window no button? I'd suggest creating a new question with a description of what you're asking krishna

– Craig Wayne
Apr 1 at 5:46





So you just want a new window no button? I'd suggest creating a new question with a description of what you're asking krishna

– Craig Wayne
Apr 1 at 5:46



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55311129%2fevent-listeners-are-not-working-on-popup-window-when-close-its-parent-window%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript