wp_enqueue_script不显示脚本

时间:2013-04-10 18:11:29

标签: javascript wordpress

这是我第一次将wp_enqueue_script用于我想在WordPress网站上设置的IDX解决方案。我的文件路径是child-theme / js / idx.js

我将此代码放在我的functions.php文件中:

<?php
function wptuts_scripts_with_jquery()  
{  

    // Register the script like this for a theme:  
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/idx.js',      array( 'jquery' ) );  

// For either a plugin or a theme, you can then enqueue the script:  
    wp_enqueue_script( 'custom-script' );  
}  
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_jquery' );  ?>

和我的idx.js文件中的代码:

<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript" ></script>
<script src="http://tools.example.com/scripts/postmessage.min.js" type="text/javascript" ></script>
<script type="text/javascript" >
$.createDynamicFrame('http://example.example.com', 'http://example.example.com/26611/Carets/search?parentUrl=' + encodeURIComponent( document.location.href ), 'MapSearch');

这没有任何作用。当我在functions.php中使用wptuts_scripts_with_jquery()函数和其他脚本时,似乎可以工作,但不能使用idx.js脚本。任何帮助都会非常感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道为什么你要在外部资源中包含<script>标签,但这一切都错了 - 它们应该只包含纯javascript(这是浏览器已经预料到的)。

您的idx.js文件应仅包含以下行:$.createDynamicFrame(...


这就是为什么不包含这个错误文件的原因 - 在Wordpress中排队脚本时作为参数传递的数组是一个依赖项列表。我假设,因为你试图在你的JS文件中包含jQuery - 然后它以前没有被模板加载?在这种情况下,因为你没有入队 jQuery - “自定义脚本”没有所有必需的组件,所以它不会被推到页面上

默认情况下,wordpress已经注册了jQuery版本 - 如果是CDN首选项(或向后兼容性),你需要使用不同的版本,那么你应该wp_deregister_script并使用你想要的版本重新排列它。否则,您的功能应如下所示:

function wptuts_scripts_with_jquery(){
  $path = get_template_directory_uri();
  wp_enqueue_script('jquery');
  wp_enqueue_script('custom-script', "{$path}/js/idx.js", array('jquery'));  
} 

由于tools.example.com/scripts/postmessage.min.js没有任何内容,我将其排除在外。另请注意,您可以在单个wp_enqueue_script语句中注册入队

相关问题