页脚中的 JavaScript 函数

JavaScript functions in footer

本文关键字:JavaScript 函数      更新时间:2023-09-26

我目前正在使用html5样板,它将jQuery和你的插件放在页脚中。 唯一的问题是,我在每页中都使用相同的页脚.php文件。 如果插件尚未加载,如何包含特定于页面的功能? 即

<?php include_layout_template('header.php', '../');?>
<div id='mediaspace'>This text will be replaced</div>
<!-- Video for this page: -->
<script type='text/javascript'>
  jwplayer('mediaspace').setup({
  'flashplayer': '../resources/player.swf',
  'file': 'http://content.longtailvideo.com/videos/flvplayer.flv',
  'controlbar': 'bottom',
  'width': '470',
  'height': '320'
});
</script>
<?php //This file has the jwplayer function in it: 
include_layout_template('footer.php', '../'); ?>

这样做我得到

Uncaught ReferenceError: jwplayer is not defined 

将插件包含在标题中会更容易吗?人们通常如何在最后的样板/加载脚本中处理这样的事情?

你试过document.onready()函数吗?这样脚本将在页面准备就绪后触发。

"jwplayer is not defined" ReferenceError 正是它所说的。 在加载 jwplayer.js 文件之前调用您的 jwplayer 引用,该文件定义了您的 jwplayer 函数。

如果你无法将 jwplayer.js 文件移动到文档的头部(因此它在调用 jwplayer() 之前加载),那么你需要重新考虑如何以及何时调用此脚本。

我假设"媒体空间"引用了对象/div 的 id? 如果是这样,请将其保留在页面中间的位置,并将脚本移动到页脚下方.php包含。