Window.open and window.print blocking JavaScript - javascript

I have a site where a particular page has two buttons that open pages in new tabs. One page is a preview of an item, and one is the print of that item.
// Preview button's click event
window.open('my.preview.url.com', 'preview');
// Print button's click event
window.open('my.print.url.com, 'print');
On the print page, window.print(); is call on page load.
This seems to create a bit of an issue on some browsers. Opening the print page and not closing the print preview results in all of the JavaScript being blocked on the originating page (with the buttons) and the preview page (if one had been opened previously).
This appears to not happen in all browsers. For example, Chrome displays the issue but Firefox does not.
This is similar to a similar unanswered post, which has a useful fiddle at http://jsfiddle.net/Zicane/7ntsb7hh/10/
Hoping someone might know the solution to both issues.

Related

Wordpress, how to debug Javascript to check a specific behavior

I need to debug Javascript to check which function is called on a specific behavior.
To be more specific, when we click inside an iframe, all the page or at least the iframe is reloaded, so loaded 2 times.
For example, if you take a look at this page:
https://artetcadres.fr/art-et-images/#collection/hopper/
and click on the artist "Klimt", the main page activate a javascript function, display this image on the entire page and reload the iframe content.
I tried with the Chrome Devtools but can't figure out which function is called display the image "Loading..." and reload the iframe.
It's reloaded maybe because the url is dynamically updated. The anchor #collection/hopper/ will become #collection/klimt/.
This strange behavior appeared after an wordpress upgrade.

How to detect print preview modal open in child page?

Question:
From Page A I have a link to Page B, on Page B it calls window.print() on load, which opens the print preview modal. My question is on Page A, is there an easy way to know that the print preview modal on Page B is currently open or not? (I only need this to work on Chrome)
Background:
The reason I need to get this done is that there's a problem with Google Chrome blocking parent page's Ajax calls when it has child page who has print preview modal open.
Related Questions:
Chrome browser unable to make a server call when print preview is opened
Google Chrome blocks ajax requests when print preview is opened on child window
https://code.google.com/p/chromium/issues/detail?id=139706
There's no easy way to completely solve the problem, so I only want to know whether the print preview modal is open and warn users to close the print modal on Page B before moving forward on Page A.
This looks to not be possible. Chrome doesn't just blocks ajax execution, but it also stops processing all scripts on page A.
I think your best bet is to have instructions on page B for the user to print the page. Any Javascript initiated print will pause Javascript execution on page A.
A possible solution would be to display a message on page A before opening page B in anticipation of this behavior.
Edit: pulling in information from comment below
After further thinking, the best solution would be: On page B, in the script, that calls window.print(), just before calling print, send information back to page A communicating that this is going to take place, so that page A may take the appropriate action.

Chrome Extension: How to run Javascript on page load (without popup open)

I have a Chrome extension that does word substitutions on pages. Currently I have a popup that opens when I click on a browser action which lets the user control whether or not to perform the substitutions. I also have a background script running that sends a message to content scripts when a page refresh happens using a chrome.tabs.onUpdated event handler.
My problem is that when I refresh the page no substitutions are made unless the popup is open (which is only possible when I have the inspect elements panel open on the popup, because otherwise the popup closes when I refresh).
Has anybody had experience with this behavior before? What additional instrumentation should I add to diagnose the problem? Is there a different extension architecture/code arrangement I should be using?
Thanks in advance!
If you use localStorage on a given page you can probably store whether or not to substitute words. I think refreshing a page is similar to opening a new tab, so your script gets reinjected/reloaded rather than staying open and receiving an onupdate message.

window.history.back not working on new window with Firefox

I have a page with a link that opens a new window with a div and an iframe. The user can navigate through the web that it's inside the iframe and the div shows information.
The user wants an option to go back on pages that he visited inside the iframe, so I put an image on the div to allow that option. The image has the onClick event that calls history.back() method.
The problem that I have it's that in Firefox when I try to go a page back, the iframe stills showing the page that I'm seeing but if I try in Chrome it works great, I can visit all the sections that has my page and I can go back clicking on that image.
I tried to use the browser's context menu but in Firefox does nothing.

IFrame Back button

I searched a lot to get rid of this problem on the internet but could not find a specific solution despite the problem being discussed in details previously.
The query is simple. My javascript dynamically adds an Iframe to the web page (which displays a feedback form). The problem is that, "after answering", now when the user clicks the back-button of the browser the iframe instead of the browser window is affected i.e. the questionnaire is displayed again. I want the browser back button to behave normally.
This behavior is really annoying and I am having real trouble fixing this.
I am using firefox.
Looking forward to the replies. Please inform me if I should give more details.
Thanks,
Your form has a submit button, which posts the page to the server. The back button will always send the user back to the form regardless of whether you use a iframe or not. The ideal way is to notify the user of a completed action, in this case thank the user for the feedback (using an alert box) and redirect the user to the home page or provide a button in the page saying "Back to Home".
Firefox and IE indeed act like you mentioned, but Chrome do not, and I'd guess other WebKit browsers would do the same.
In Chrome, clicking the Back button will land you where you want to go (the previous URL of the parent frame). i.e. Chrome to not add iframe URL changes in the back button history.
Sadly, I've found no way to force IE and FF to replicate this, so I used the AJAX post approach suggested above by Arun.
Here's my iframe source, which use jQuery to post the form, and replace the whole page with the result of that POST:
<form method="post" onsubmit="postForm(this);return false">
...
</form>
<script type="text/javascript">
function postForm(form) {
$.post(form.action, $(form).serialize(), postCompleted);
}
function postCompleted(data) {
$('html').html(data);
}
</script>
This works in all browsers; clicking the Back button will send you back to the previous URL a seen by the end user, instead of the initial form loaded dynamically in the iframe.
I encountered the same problem: I use a dynamically created iframe to show a "popup" on my page, whose SRC points to another page that has got a form and a submit button. After submitting that page, a JS callback is used to hide the iframe. As you explained, this causes a new entry to be added to the history (on IE at least).
But I found out that removing the iframe element from the DOM (instead of hiding it) results in the unwanted history entry being removed (tested on IE9)! Which is what the user would expect in that situation.
You can observe this yourself on IE9:
Open the back button menu (right-click the back button): you only have one entry for the current page
Press submit in the iframe => the back button menu shows one extra entry for the iframe
Remove the iframe from the DOM => the back button menu no longer shows that entry

Categories

Resources