外部js脚本

时间:2016-03-27 00:09:33

标签: javascript php html css

到目前为止,我在html / css / php / js中创建了这个聊天室,但它需要很多工作,网址是here 我有很多的PHP和一些JavaScript和我想知道如何链接外部js文件我以为我做但但它不工作我已经尝试过

<script src="script.js" type="text/javascript">

方法,但它似乎没有工作

这是我的js文件中的代码

var something = document.getElementById('gamerchat');

something.style.cursor = 'pointer';
something.onclick = function() {
window.location = 'http://billischill.ddns.net/gamerchat.php'
};

var something = document.getElementById('Techroom');

something.style.cursor = 'pointer';
something.onclick = function() {
window.location = 'http://billischill.ddns.net/spygame/spygame.html'
};

这是我的整个页面:

<div id="sidebarright">
<div id="defaultchat"class="list"><p>Default Room<p></div>
<div id="gamerchat"class="list"><p>Gamer Chat<p></div>
<div id="Techroom"class="list"><p>Tech Room<p></div>
<ul>

</div>

<script src="script.js" type="text/javascript"></script>
<?
session_start();

if(isset($_GET['logout'])){ 

//Simple exit message
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has  left the chat session.</i><br></div>");
fclose($fp);

session_destroy();
header("Location: index.php"); //Redirect the user
}

function loginForm(){
echo'
<div id="loginform">
<form action="index.php" method="post">
    <p>Please enter your name to continue:</p>
    <label for="name">Name :</label>
    <input type="text" name="name" id="name" /></br></br>
    <label for="adminpass">Admin?</label>
    <input type="text" name="adminpass" /></br></br>
    <input type="submit" name="enter" id="enter" value="Enter" />
</form>
</div>
';
}

if(isset($_POST['enter'])){
if($_POST['name'] != ""){
    $_SESSION['name'] =    stripslashes(htmlspecialchars($_POST['name']));
}
else{
    echo '<span class="error">Please type in a name</span>';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chat - BillIsChill Network</title>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>


<?php
if(!isset($_SESSION['name'])){
loginForm();
}
else{
?>

<div id="wrapper">
<div id="menu">
    <p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?>     </b></p>
    <p class="logout"><a id="exit" href="#">X</a></p>
    <div style="clear:both"></div>
</div>  
<div id="chatbox"><?php
if(file_exists("log.html") && filesize("log.html") > 0){
    $handle = fopen("log.html", "r");
    $contents = fread($handle, filesize("log.html"));
    fclose($handle);

    echo $contents;
}
?></div>

<form name="message" action="">
    <input name="usermsg" type="text" id="usermsg" size="63" />
    <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
    </form>
    </div>

    <script type="text/javascript"   src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">       </script>
    <script type="text/javascript">
    // jQuery Document
    $(document).ready(function(){
    //If user submits the form
    $("#submitmsg").click(function(){   
    var clientmsg = $("#usermsg").val();
    $.post("post.php", {text: clientmsg});              
    $("#usermsg").attr("value", "");
    return false;
    });

    //Load the file containing the chat log
    function loadLog(){     
    var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
    $.ajax({
        url: "log.html",
        cache: false,
        success: function(html){        
            $("#chatbox").html(html); //Insert chat log into the      #chatbox div              
            var newscrollHeight = $("#chatbox").attr("scrollHeight")    - 20;
            if(newscrollHeight > oldscrollHeight){
                $("#chatbox").animate({ scrollTop: newscrollHeight },   'normal'); //Autoscroll to bottom of div
            }               
        },
        });
        }
        setInterval (loadLog, 2500);    //Reload file every 2.5 seconds

        //If user wants to end session
        $("#exit").click(function(){
        var exit = confirm("Are you sure you want to end the session?");
        if(exit==true){window.location = 'index.php?logout=true';}      
        });
        });



        <?php
        }
        ?>
        </body>

1 个答案:

答案 0 :(得分:0)

这应该足以加载文件。

<script src="/path/to/javascript/file.js" type="text/javascript">

但是,必须完全加载才能调用其中的任何函数。仅仅因为页面被加载和渲染并不意味着所有其他非渲染项都被完全加载。您可以使用一些策略来测试它。

您可以检查文件中声明的功能是否可用

function isFunction(fn){
    return typeof fn == 'function'
}

或使用jQuery

jQuery.isFunction(YourFunction)

您也可以使用此

$(document).ready(function(){
    //your stuff here
}

请记住,如果你已经加载了jQuery,那就不会发生冲突&#34;模式您将使用 jQuery 而不是 $

希望这有帮助,