Search code examples
javascriptphpjquerylaravel-5sweetalert2

Loading html inside sweet alert?


Here is my code for callling Swal:

window.swal({
    title: "Checking...",
    text: "Please wait",
    imageUrl: "{{ asset('media/photos/loaderspin.gif') }}",
    showConfirmButton: false,
    allowOutsideClick: false
});
$.ajax({
    type: 'POST',
    url: '/' + target,
    data: {
        data: true
    },
    success: function(response) {
        window.swal({
            type: 'info',
            width: 900,
            padding: '3em',
            html: response,
            showCloseButton: true,
            focusConfirm: false,
        });
    },
    error: function(response) {
    }
});

Basically I'm making an ajax request that does a return view('index', compact('data')); in the controller.

The html that I'm trying to load inside Swal has a <script src="my-file.js"></script> inside it, but it does not get loaded when Swal is fired, so no events get fired when I click inside.

I've tried looking in the docs but I can't find anything. How will I get swal to load that file?


Solution

  • I was able to solve this by adding this line inside the success block:

    $.getScript('my-file.js')