0

It is not recognizing the swal from SweetAlert because it is not finding the js file. I cannot do it the conventional way in the header for html for certain reasons. Is there any way to get this to work?

This code gives the error sweetalert2.min.js:1Uncaught TypeError: Cannot read property 'appendChild' of null

I have tried moving the script for the js all over the place but still doesn't work. Any help would be great. Thanks.

function alert_swal($title, $message, $type)
{
    //External CSS and JS
    echo '<link href="https://cdn.jsdelivr.net/sweetalert2/4.0.5/sweetalert2.min.css" rel="stylesheet">';
    echo '<script src="https://cdn.jsdelivr.net/sweetalert2/4.0.10/sweetalert2.min.js"></script>';

    echo '<script type="text/javascript">';
    echo 'swal(\'' . $title . '\',\'' . $message . '\',\'' . $type . '\');';
    echo '</script>';
}
4
  • 1
    The error message doesn't say anything about not recognizing swal or not finding the js file. It sounds more like it doesn't find a DOM node it tries to work on. Commented Jul 23, 2016 at 19:17
  • echo '<body>'; echo '<link href="cdn.jsdelivr.net/sweetalert2/4.0.5/sweetalert2.min.css" rel="stylesheet"/>'; echo '<script src="cdn.jsdelivr.net/sweetalert2/4.0.10/…>'; echo '<script type="text/javascript">'; echo 'swal(\'' . $title . '\',\'' . $message . '\',\'' . $type . '\');'; echo '</script>'; echo '</body>'; Commented Jul 23, 2016 at 19:25
  • @MujeebuRahman, your solution works. Can't believe it was so simple. Thanks very much. Commented Jul 23, 2016 at 19:33
  • @kooliebwoy : updated the answer Commented Jul 23, 2016 at 19:45

2 Answers 2

1
echo '<body>';
echo '<link href="https://cdn.jsdelivr.net/sweetalert2/4.0.5/sweetalert2.min.css" rel="stylesheet"/>';
echo '<script src="https://cdn.jsdelivr.net/sweetalert2/4.0.10/sweetalert2.min.js"></script>';
echo '<script type="text/javascript">';
echo 'swal(\'' . $title . '\',\'' . $message . '\',\'' . $type . '\');';
echo '</script>';
echo '</body>';
Sign up to request clarification or add additional context in comments.

Comments

1

You should write this

function alert_swal($title, $message, $type)
{
    //External CSS and JS
    echo '<body>';
echo '<link href="https://cdn.jsdelivr.net/sweetalert2/4.0.5/sweetalert2.min.css" rel="stylesheet"/>';
echo '<script src="https://cdn.jsdelivr.net/sweetalert2/4.0.10/sweetalert2.min.js"></script>';
echo '<script type="text/javascript">';
echo 'swal(\'' . $title . '\',\'' . $message . '\',\'' . $type . '\');';
echo '</script>';
echo '</body>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.