I’m creating a web application with Django Channels and WebSockets. When a WebSocket connection is open, I want to dynamically create Bootstrap toasts (https://getbootstrap.com/docs/4.2/components/toasts/). However, an error is being raised: Uncaught TypeError: $(...).toast is not a function
.
The Socket.onopen
line $('.toast').toast('show');
is not being read correctly. I am not familiar with jQuery.
Here is a part of the js file:
var Socket = new ReconnectingWebSocket(ws_path)
Socket.onopen = function (e) {
newToast();
$('.toast').toast('show');
};
function newToast(data) {
// create Bootstrap toast element
};
The head of html file contains the following Bootstrap and jQuery files:
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
I have already tried replacing the dollar sign with jQuery, but the same error occurs. How can I read the $('.toast').toast('show');
line in Socket.onopen
? Any ideas would be appreciated.