Execute script only on certain admin pages

To solve this problem i’ve used this in my js file:

var location = String(window.location);

//only runs in post.php and post-new.php
if(location.search('post.php') != -1 || location.search('post-new.php') != -1 )
{

}

But it’s does not seem like i solid solution. Are there any other way?

Related posts

Leave a Reply

1 comment

  1. You can use this in your functions.php :

    function add_admin_scripts( $hook ) {
    
        if ( $hook == 'post-new.php' ) {
    
            wp_enqueue_script(  'myscript', get_bloginfo('template_directory').'/js/myscript.js' );
    
        }
    }
    add_action('admin_enqueue_scripts','add_admin_scripts',10,1);