繁体   English   中英

如何停止页面之间的简单JavaScript冲突?

[英]How to stop a simple javascript conflict between pages?

所以我有2页top.php和Currentpage.php。 首页上有最新版本的Jquery和Bootstrap。 当前页面包含我暂时无法升级的代码。 有没有办法在加载top.php Javascript代码后停止它,然后加载currentpage.php Javascript?

top.php具有:

<html lang="en">
<head>
<!-- Bootstrap -->
<script src="../bootstrap/jquery/1.12.2/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="../bootstrap/js/bootstrap.min.js"></script> 
</head>
<body> 
<!-- some code here for top navigation -->

Currentpage.php具有:

<head>
<script type="style/js/jquery.form-2.47.js"></script>
    <script type="text/javascript" src="style/js/jquery.ui.widget.js"></script>
    <script type="text/javascript" src="style/js/jquery.ui.autocomplete.js"></script>
    <script src="style/js/jquery-ui-1.8.1.dialog.min.js"></script>
    <script type="text/javascript" src="index.js"></script>

</head>
<body>
<?php
include "../includes/top.php";
?>
<!-- stop the top.php code here then load the current page Javascript code -->
</body>

我使用一个变量告诉top.php不要加载jQuery。

top.php:

<html lang="en">
<head>
<?php if (!isset($jquery_loaded)) { ?>
    <!-- Bootstrap -->
    <script src="../bootstrap/jquery/1.12.2/jquery.min.js"></script>
<?php } ?>

<!-- Bootstrap Core JavaScript -->
<script src="../bootstrap/js/bootstrap.min.js"></script> 
</head>
<body> 
<!-- some code here for top navigation -->

Currentpage.php:

<head>
    <script type="style/js/jquery.form-2.47.js"></script>
    <script type="text/javascript" src="style/js/jquery.ui.widget.js"></script>
    <script type="text/javascript" src="style/js/jquery.ui.autocomplete.js"></script>
    <script src="style/js/jquery-ui-1.8.1.dialog.min.js"></script>
    <script type="text/javascript" src="index.js"></script>

</head>
<body>
<?php
$jquery_loaded = true;
include "../includes/top.php";
?>
<!-- stop the top.php code here then load the current page Javascript code -->
</body>

一种方法是读入文本文件(与使用include相对)并删除Javascript行,然后写出内容。 像这样:

$str=file_get_contents('top.php');
$str=str_replace("<script src=\"../bootstrap/jquery/1.12.2/jquery.min.js\"></script>", "",$str);
$str=str_replace("<script src=\"../bootstrap/js/bootstrap.min.js\"></script>", "",$str);
echo $str;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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