简体   繁体   中英

call js method only on wordpress shortcode page

I want to call a js method only that page contains the same shortcode I passed on another page of shortcode that function should not execute....

for example, I have two shortcodes on different pages. when I go to the 2nd page then 1st-page shortcode method fired and return an error in console log that some ID is not found...

have a look a code

Note I'm using Gulp

my first code

1st shortcode js

class EmiCalculator {
//construct
constructor() {
    this.calculateEMI();
    this.events();
    this.footerTitle();
}

this.calculateEMI(); return error when I go to other TypeError: document.getElementById(...) is null

so I want when I got another page I want this.calculateEMI(); method not fire...

You can register a js script file like-

wp_register_script( 'js-name',plugin_dir_url( __FILE__ ) .'assets/js/yourfile.js',array ('jquery'),'',true );

and after that you can enqueue in your shortcode function. you can add like this-

function shortcode() {
wp_enqueue_script('js-name');
//code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM