Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window confirm() method throws a JS error when used in a metabox #3169

Closed
kmgalanakis opened this issue Oct 26, 2017 · 1 comment
Closed
Labels
[Feature] Meta Boxes A draggable box shown on the post editing screen

Comments

@kmgalanakis
Copy link

kmgalanakis commented Oct 26, 2017

When creating a metabox the content of it is sandboxed. If this metabox contains a button that when clicked it asks for a user confirmation using the Window confirm() method, a JS error is thrown stating that:

Ignored call to 'confirm()'. The document is sandboxed, and the 'allow-modals' keyword is not set.

On the test code below, I'm adding the custom metabox, which contains a button. Then I'm enqueuing a small JS script that handles the click of that button and asks for the user confirmation.

Test code

add_action( 'admin_head', 'my_meta_box' );
function my_meta_box() {
	global $post;
	add_meta_box(
		'my_meta_box',
		__( 'My metabox', 'my-context' ),
		'my_meta_box_callback',
		$post->post_type, 'side'
	);
}

function my_meta_box_callback() {
	echo '<p>Lorem ipsum dolor sit amet.</p>';
	echo '<button type="button" class="js-click-class">Click Me!</button>';
}

add_action( 'admin_footer', 'custom_admin_js' );
function custom_admin_js() {
    $script = '
         jQuery(".js-click-class").on("click", function () {
            confirm( "Are you sure you want to continue?" );
        });
    ';
	echo '<script type="text/javascript">' . $script . '</script>';
}
@BE-Webdesign BE-Webdesign added the [Feature] Meta Boxes A draggable box shown on the post editing screen label Nov 2, 2017
@BE-Webdesign
Copy link
Contributor

Hi kmgalanakis,

Should be relatively easy to work around. Thank you for the detailed report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Meta Boxes A draggable box shown on the post editing screen
Projects
None yet
Development

No branches or pull requests

3 participants