IE11中swal().then()回调无法触发的技术求助
.then() Not Triggering Hey there, let's break down why your SweetAlert2's .then() isn't firing in IE11 and walk through actionable checks to fix it:
1. Verify the ES6 Promise Polyfill is Actually Working
You're loading es6-promise@3.2.1, but many older promise polyfills require an explicit initialization call to attach to the global scope in IE11. Add this line right after the script tag for es6-promise:
<script src="https://npmcdn.com/es6-promise@3.2.1"></script> <script>ES6Promise.polyfill();</script>
Without this, the Promise object might not be available globally when SweetAlert2 loads, so its promise-based API won't function as expected.
Also, confirm the polyfill is loading at all:
- Open IE11's F12 DevTools > Network tab
- Refresh the page and check if
es6-promise@3.2.1appears with a 200 status - If it's missing, your server-side
Request.Browsercheck might not detect IE11 correctly. IE11's user agent string can be tricky—try updating the condition to include checks forTrident, or switch to a client-side check likeif (typeof Promise === 'undefined')to load the polyfill only when needed.
2. Validate SweetAlert2's IE11 Compatibility
Your current setup uses the standard SweetAlert2 build, but ensure you're on a version that explicitly supports IE11. Older versions might have broken promise handling in legacy browsers. Also, double-check that the IE9-specific script isn't accidentally loading in IE11—your conditional comment <!--[if IE 9]> should prevent this, but it's worth confirming in the Network tab.
For testing, try using the sweetalert2-ie9 build for IE11 (remove the standard SweetAlert2 script first to avoid conflicts):
<!--[if lte IE 11]> <script src="~/bower_components/sweetalert2-ie9/dist/sweetalert2.min.js"></script> <![endif]-->
3. Fall Back to SweetAlert2's Callback Syntax
Older SweetAlert2 versions had better support for traditional callbacks in IE11 than promise chaining. Rewrite your code to use the callback parameter instead of .then():
swal({ title: translations.AreYouSureYouWantToSubmit, text: '', type: 'warning', showCancelButton: true, confirmButtonText: '<i class="fas fa-thumbs-up"></i> ' + globalTranslations.Yes, cancelButtonText: '<i class="fas fa-thumbs-down"></i> ' + globalTranslations.No, confirmButtonClass: 'btn btn-success', cancelButtonClass: 'btn btn-danger', buttonsStyling: false }, function(isConfirmed) { if (isConfirmed) { // Your original .then() logic goes here vm.state($(event.currentTarget).data('state')); // ... rest of your validation and submission code } });
This bypasses promise-related issues entirely and is a reliable fallback for IE11.
4. Check for Silent Errors in IE11 DevTools
IE11's console might hide errors that break the promise chain. Open F12 DevTools > Console tab, enable "Errors" filtering, and refresh the page. Look for:
"Promise is undefined"(confirms the polyfill isn't working)- Script errors from SweetAlert2 itself
- Silently failing validation logic that stops execution before
.then()triggers
5. Ensure No Scripts Overwrite the Global Promise Object
Some third-party scripts might overwrite the global Promise in IE11. In DevTools > Console, run typeof Promise—it should return "function". If it returns "undefined" or another value, another script is interfering with your polyfill.
内容的提问来源于stack exchange,提问作者Glitch_Doctor

