未捕获的ReferenceError |无法访问WordPress中外部文件中定义的JavaScript变量和函数

问题描述

我已经使用add_action('wp_enqueue_scripts','function_name')将外部JS文件放入wordpress function.PHP中,浏览器获取了JS文件,并且在Debugger中也可以看到该文件

 Contents of the external JS are visible as seen in the picture

但是我无法调用文件中定义的函数,也无法访问常量和变量。

But i'm unable to access the constants,as seen here

解决方法

经过长期调查,我发现了问题。并修复它。

问题:外部JS文件被嵌入到iframe(Wordpress Customizer Preview。)中,而我试图从Customizer Controller进行访问。

解决方案:我使用add_action('customize_controls_enqueue_scripts','function_name');

进入外部JS文件。