简体   繁体   English

所需php中的Javascript文件不起作用,但直接在文件中起作用

[英]Javascript files in a requiered php does not work, but directly in the file does

I have an index.php file that requires a common head.php file, in the head.php file are several Javascript files, when trying it like that, the code looks fine in the source, but the files don't actually do anything to the document. 我有一个index.php文件,它需要一个通用的head.php文件,在head.php文件中是几个Javascript文件,当这样尝试时,代码在源代码中看起来还不错,但是这些文件实际上没有做任何事情到文档。 (The .css file works fine though) (.css文件可以正常工作)

If the Javascript files are put directly into the index.php (in the same head area as where the head.php is required) they work fine, even though the source show the exact same code. 如果将Javascript文件直接放入index.php中(与head.php所在的头部相同),即使源代码显示的代码完全相同,它们也可以正常工作。

Is there a trick to make this work properly, or do I have to put the Javascript manually into every single page on the website? 是否有使之正常工作的技巧,还是必须将Javascript手动放入网站的每个页面中? (There are a lot and I would like to be able to use a single head.php just in case I want to update a file or add a script) (有很多,我希望能够使用单个head.php,以防万一我想更新文件或添加脚本)

Any help would be very appreciated! 任何帮助将不胜感激!

Below is some code for the different pages for reference: 以下是不同页面的一些代码供参考:


head.php head.php

<link rel="stylesheet" type="text/css" href="style/main.css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="include/alajax.js"></script>
<script type="text/javascript" src="include/jtip.js"></script>
<script type="text/javascript" src="include/jquery.timeago.js"></script>

index.php index.php

<head>
<?php require_once 'site/head.php'; ?>
</head>

index.php (Source Code as displayed by the browser) index.php (浏览器显示的源代码)

<head>
<!--require_once 'site/head.php';-->
<link rel="stylesheet" type="text/css" href="includes/fancy/fancy.css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="include/alajax.js"></script>
<script type="text/javascript" src="include/jtip.js"></script>
<script type="text/javascript" src="include/jquery.timeago.js"></script>
<!-- end of head.php -->
</head>
<body>
<!-- Site that uses jquery and other functions in the included javascript that do not work with the files when included through a .php, though works fine if directly on the same page as the functions -->

EDIT: (Here is a more fleshed out index.php with the header.php and login.php) 编辑:(这是带有header.php和login.php的更加充实的index.php)

index.php index.php

<head>
    <?php require_once 'site/head.php'; ?>
</head>
<body>
    <div id="header">
        <?php require 'site/header.php'; ?>    
    </div>
    <div id="wrapper">
        <div id="navbar">
            <?php require 'site/navbar.php'; ?>    
        </div>
        <div id="content">
            <!-- Just basic php and html here, and some times that rely on jquery.timeago.js to calculate time, works fine when everything is loaded in head.php -->
        </div>
    </div>
</body>

header.php (not to be confused with head.php) header.php (不要与head.php混淆)

<div id="loginheader">
    <img src="img/logo.png" width="200" height="100" alt="Logo" />
    <?php
        require 'login.php';
    ?>
</div>

login.php login.php

<!--- Contains javascript that requires jquery (included in head.php) and alajax (also included in head.php) if jquery is loaded in here and not in head, the form works, but the times in index.php break. -->

Hi folks finally i found the answer, the reason behind js file not loading is the problem with my/your one of the js file which we have include. 大家好,我终于找到了答案,js文件未加载的原因是我/您所包含的js文件之一存在问题。 So please check all the js files one by one for identifying the corrupted one. 因此,请一一检查所有js文件,以识别损坏的js文件。

Explanation of my case. 我的情况的解释。

I have included all the css and js files inside a file called html_header.php 我已经将所有css和js文件包含在名为html_header.php的文件中

<script type="text/javascript" src="../js/jquery-1.7.2.min.js"> </script>
<script type="text/javascript" src="../js/excanvas.js"> </script>
<script type="text/javascript" src="../js/jquery.flot.js"> </script>
<script type="text/javascript" src="../js/jquery.flot.stack.js"> </script>
<script type="text/javascript" src="../js/jquery.flot.pie.js"> </script>
<script type="text/javascript" src="../js/jquery.quicksand.js"> </script>
<script type="text/javascript" src="../js/jquery.easing.1.3.js"> </script>
<script type="text/javascript" src="../js/jquery.tipsy.js"> </script>
<script type="text/javascript" src="../js/cl_editor/jquery.cleditor.min.js"> </script>
<script type="text/javascript" src="../uploadify/swfobject.js"></script>
<script type="text/javascript" src="../uploadify/jquery.uploadify.v2.1.4.min.js"></script>
<script type="text/javascript" src="../js/jquery.autogrowtextarea.js"></script>
<script type="text/javascript" src="../js/form_elements.js"></script>
<script type="text/javascript" src="../js/jquery.ui.core.js"></script>
<script type="text/javascript" src="../js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="../js/jquery.ui.mouse.js"></script>
<script type="text/javascript" src="../js/jquery.ui.slider.js"></script>
<script type="text/javascript" src="../js/jquery.ui.progressbar.js"></script>
<script type="text/javascript" src="../js/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="../js/jquery.ui.tabs.js"></script>
<script type="text/javascript" src="../js/jquery.ui.autocomplete.js"></script>
<script type="text/javascript" src="../js/jquery.ui.position.min.js"></script>
<script type="text/javascript" src="../js/fullcalendar.js"></script>
<script type="text/javascript" src="../js/gcal.js"></script>
<script type="text/javascript" src="../js/bootstrap-modal.js"></script>
<script type="text/javascript" src="../js/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript" src="../js/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
<script type="text/javascript" src="../js/highlight.js"></script>
<script type="text/javascript" src="../js/jq_tables/jquery.dataTables.js"></script>
<!--<script type="text/javascript" src="../js/main.js"> </script>-->
<script type="text/javascript" src="../js/jquery.validate.min.js"></script>
<script type="text/javascript" src="../js/jquery.printElement.min.js"></script>
<script type="text/javascript" src="../js/addmethod.js"></script>

when i commented main.js the problem solved. 当我评论main.js时,问题解决了。 After i have uploaded a fresh copy of main.js file. 在我上传了main.js文件的新副本之后。

Thank you 谢谢

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

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