I have updated Chrome to 109.0.5414.75 (Official Build) (64-bit)
It seems that window.onbeforeprint works fine but I need window.onafterprint to fire.
The code still works on Edge/Firefox etc so I can only think it has broken in the last update.
window.onafterprint = () => { console.log('after!'); }
The following code works great in Chrome 9+ and Safari 5.1. However, it doesn’t work in Firefox or IE10,
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
console.log('onbeforeprint equivalent');
} else {
console.log('onafterprint equivalent');
}
});
Related
I've been trying to send a beacon on beforeunload and it seems to work on pretty much all modern browsers, except Chrome in incognito mode.
This is the code that works in all modern browsers, except Chrome in incognito mode:
window.onbeforeunload = function() {
navigator.sendBeacon("url");
}
Not even this code doesn't seem to work:
window.onbeforeunload = function() {
console.log('before unload')
}
Am I doing anything wrong or is it just Chrome's fault?
What's your setup (SO, Chrome version) ?
On Chrome 80.0.3987.116 / Ubuntu 64 and Chrome 79.0.3945.130 / Windows 10 the below snippet works falwlessy:
window.addEventListener('beforeunload', (event) => {
console.log("BEFORE")
navigator.sendBeacon("http://www.google.it");
// Cancel the event as stated by the standard.
event.preventDefault();
// Chrome requires returnValue to be set.
event.returnValue = '';
});
Screen of the request (incognito mode) sent on beforeunload:
Furthermore, notice:
To combat unwanted pop-ups, some browsers don't display prompts
created in beforeunload event handlers unless the page has been
interacted with. Moreover, some don't display them at all.
Ref: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload
Ok so the code below works on every browser except ios chrome and IE.
What changes can be made to make it compatible with those browsers
<script type="text/javascript">
!function(){
var campaign_link = "http://example.com/time&"; // REPLACE WITH YOUR LINK
var t;
try{
for(t=0;10>t;++t)history.pushState({},"","#");
onpopstate=function(t){t.state&&location.replace(campaign_link)}}
catch(o){}
}();
</script>
You are getting the error because of history API. Its not supported in sme browsers
All versions below Internet Explorer 9 definitely does not support history.pushState() or history.popState()
iOS has a fair few bug with the HTML5 History API.
You can try like :
window.addEventListener("popstate", function(e) {
window.location.href = location.href;
});
Here is the code, which is working fine on desktop browsers, but for some reason is breaking when I use it on Safari on my iPad.
var url = window.location.href;
if(url.indexOf("playground") != -1)
{
toggleDisplay(0);
alert("pgroud");
}
The url.indexOf("playground") is returning 21, I checked on iPad, so the code should be working, but the function and the alert are not being called.
I have some JQuery and JavaScript code that works in Chrome and Firefox but not IE. The code is nested inside of SAS and I removed the SAS nesting. Does anyone see a reason why this code would error out in IE? related. I am using IE 11. I am getting the error: document.addEventListener( "DOMContentLoaded", completed, false );
<script type="text/JavaScript">
function saveMultiSelection(pageName) {
document.getElementById('save_degStudLevComma').value = $(""#degStudLev"").multipleSelect('getSelects');
document.getElementById('save_studyLevelComma').value = $(""#studyLevel"").multipleSelect('getSelects');
document.getElementById('save_sumLevComma').value = $(""#sumlev"").multipleSelect('getSelects');
document.getElementById('save_awardTypeComma').value = $(""#awardType"").multipleSelect('getSelects');
}
</script>
Why you don't use jquery all the way if you have it in your app already?
function saveMultiSelection(pageName) {
//(...)
$('#save_degStudLevComma').val($("#degStudLev").multipleSelect('getSelects'));
//(...)
}
You can see broswer support version -> https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded
IE support = 9+ ->DOMContentLoaded
We have a datepicker (in JavaScript) that has a section for checking IE 8 and older and other modern browsers.
if(-1 != navigator.userAgent.indexOf("MSIE")){
obj_caller.target.fireEvent("onchange");
}
else {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
obj_caller.target.dispatchEvent(evt);
}
It's working fine in Chrome, Firefox, IE8 and below but is failing in IE 11. What I need is a way to get the else part working in IE 11. I just cannot figure out what is failing and how to fix it.
Thanks.
Your problem is that fireEvent shouldn't be used in newer IE versions. Support for dispatchEvent was added in IE9. http://help.dottoro.com/ljrinokx.php
if(document.createEventObject) {
obj_caller.target.fireEvent("onchange");
} else {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
obj_caller.target.dispatchEvent(evt);
}