在 <body> php 之前包含脚本


Include scripts before <body> php

有没有办法在php中将脚本注入到<body>标签之前的页面中?我已经看到了一些关于如何使用jquery执行此操作的示例,但是jquery导致我的网站的某些部分无法正常运行,因此我需要一个php代码,以便我可以有选择地在页面的开始标记之前注入脚本。

即。

.....
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="jquery.internads.js"></script>
<script>
    $(document).ready(function(){       
        // Call Intern Ads              
        $().tcInternAds({
            title: "Support Our Sponsors!",
            url: "adpage.html",
            timeout: 10,
            deplay: 0,
            wait: 0,
            closeable: true
        });
    });
</script>
<body>
<div class="xxxxx">
...............</div>

如果可能的话,我希望能够使用 php,这样我就可以将代码包含在需要调用脚本的模板文件中,同时避免全局包含。

谁能帮忙?

最好话筒

把你的内容放在索引中.php(或者不管它叫什么)。

它应该看起来像这样:

<?php
...phpcode...
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="jquery.internads.js"></script>
<script>
...
</script>
<body>
<div class="xxxxx">
<?php
another php code
?>
</div>

简单地将其放在<head>内不是工作吗?

<head>
.....
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="jquery.internads.js"></script>
<script>
    $(window).load()(function(){       
        // Call Intern Ads              
        $().tcInternAds({
            title: "Support Our Sponsors!",
            url: "adpage.html",
            timeout: 10,
            deplay: 0,
            wait: 0,
            closeable: true
        });
    });
</script>
</head>
<body>
<div class="xxxxx">
...............</div>

我已经找到了解决问题的方法,而无需注入或替换头代码。问题是我的网站包含mootools,而jquery与之冲突。我通过使用 jQuery noConflict 解决了这个问题,它定义了要使用的命名空间,从而通过使用自定义变量而不是美元 $ 来解决问题。

这是我解决jquery/mootools冲突的工作解决方案。

<link href="/internAds.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="/jquery.internads.js"></script>
<script>
var $j = jQuery.noConflict();
    $j(document).ready(function(){      
        // Call Intern Ads              
        $j().tcInternAds({
            title: "Support Our Sponsors!",
            url: "/adpage.html",
            timeout: 15,
            deplay: 0,
            wait: 5,
            closeable: false
        });
    });
</script>

感谢大家的时间,我不确定我是如何忽视冲突的。

最好