Pedro Antônio
Pedro Antônio

Reputation: 405

SweetAlert2 syntax error on Internet Explorer 11

I'm using the exactly code of SweetAlert2 examples page:

swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then((result) => {
  if (result.value) {
    swal(
      'Deleted!',
      'Your file has been deleted.',
      'success'
    )
  }
})

Works fine on Firefox and Chrome, but Internet Explorer shows SCRIPT1002: Syntax Error and not run the script...IE flag this portion as syntax error:

}).then((result) => {

Thanks for any help

Upvotes: 6

Views: 9141

Answers (4)

Kevin
Kevin

Reputation: 121

To make showLoading() work in IE11 you need to use the promise shim and an anonymous function...

Swal.fire({
    title: "Saving",
    text: "Please wait...",
    onBeforeOpen: function() {
        Swal.showLoading();
    }
});

Upvotes: 0

Pedro Antônio
Pedro Antônio

Reputation: 405

Aditionally to anonymous functions, to have swal fully functional in IE, its necessary add and script tag

<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/core.js"></script>

As seen in https://github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2#1-ie-support

Upvotes: 0

Limon Monte
Limon Monte

Reputation: 54399

IE11 does not support some modern ES6 features like arrow functions and promises.

To fix it, you should either compile your code with Babel, or use a Promise-polyfill with the traditional function syntax:

swal(...)
  .then(function(result) {
    console.log(result.value)
  })

Read more about SweetAlert2 usage: https://github.com/sweetalert2/sweetalert2#usage

Upvotes: 4

Rory McCrossan
Rory McCrossan

Reputation: 337560

(result) => {} is an arrow function which is completely unsupported in IE. To fix this you'll have to use a traditional anonymous function:

swal({
  // options...
}).then(function(result) {
  if (result.value) {
    swal('Deleted!', 'Your file has been deleted.', 'success');
  }
});

Upvotes: 19

Related Questions