Open ina6ra opened 2 years ago
I was able to solve myself by using requirejs. Thanks.
$$.html(`
<head>
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/sweetalert2@11.4.9/dist/sweetalert2.min.css'>
<script>
requirejs.config({
paths: {
'Swal': ['https://cdn.jsdelivr.net/npm/sweetalert2@11.4.9/dist/sweetalert2.min'],
},
});
$(document).ready(() => {
$('button').on('click', () => {
require(['Swal'], (Swal) => {
Swal.fire('Click!');
});
});
});
</script>
</head>
<body>
<p><button type='button'>Open Alert</button></p>
</body>
`);
It worked without changing the original code by registering it in the window object after loading the document.
$$.html(`
<head>
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/sweetalert2@11.4.9/dist/sweetalert2.min.css'>
<script>
requirejs.config({
paths: {
'Swal': ['https://cdn.jsdelivr.net/npm/sweetalert2@11.4.9/dist/sweetalert2.min'],
},
});
$(document).ready(() => {
require(['Swal'], (Swal) => {
window.Swal = Swal;
});
$('button').on('click', () => {
Swal.fire('Click!');
});
});
</script>
</head>
<body>
<p><button type='button'>Open Alert</button></p>
</body>
`);
@ina6ra Thank you for posting this. I'm sure iJavascript users will find it useful.
@n-riesco Thanks. I hope it helps someone.
I was able to solve this problem but another problem arose. I cannot paste in the input field displayed in the alert. Can be copied and cut.
As with the previous problem, the problem only happens with Jupyter Notebook. Rewriting to python code doesn't help.
import IPython
display(IPython.core.display.HTML('''
<head>
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/sweetalert2@11.4.9/dist/sweetalert2.min.css'>
<script>
requirejs.config({
paths: {
'Swal': 'https://cdn.jsdelivr.net/npm/sweetalert2@11.4.9/dist/sweetalert2.min',
},
});
$(document).ready(() => {
require(['Swal'], (Swal) => {
window.Swal = Swal;
});
$('button').on('click', () => {
Swal.fire({
title: 'Click!',
html: '<input class="test" size=5 value="test" />',
});
});
});
</script>
</head>
<body>
<p><button type='button'>Open Alert</button></p>
</body>
'''))
It worked on Android Chrome, so I changed the browser on my PC and it worked on Firefox. Edge doesn't work either, so it doesn't seem to work on Chromium-based browsers.
If I run the same code in Google Colaboratory it works in all browsers, so it seems to be a problem specific to Jupyter Notebook.
There are many ways to prevent copy. The DOM inspector could give you a clue of what method (event handler, css) has been used to prevent copy.
The following code works in JupyterLab but not in Jupyter Notebook.
SweetAlert2 works when opened in JupyterLab
http://my-ip-address:port/lab/tree/Untitled.ipynb
Doesn't work with Jupyter Notebook
http://my-ip-address:port/notebooks/Untitled.ipynb
The Chrome DevTools Console Panel shows an error.
Please tell me how to resolve.
It is the operating environment. I building a Miniconda3 environment with priority given to the conda-forge channel.