如何阻止页面之间的简单javascript冲突?

时间:2016-06-03 15:22:59

标签: javascript php jquery html twitter-bootstrap

所以我有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>

2 个答案:

答案 0 :(得分:2)

我使用变量告诉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>

答案 1 :(得分:1)

一种方法可能是读取文本文件(而不是使用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;