代码之家  ›  专栏  ›  技术社区  ›  graham.reeds

脚本在localhost中不工作

  •  2
  • graham.reeds  · 技术社区  · 15 年前

    我找到这个了 site/tutorial/demo

    非常好和干净的代码。但是,当从localhost运行时,我遇到了问题,在localhost中我测试了对我的站点的所有更改。我正在运行几乎完全相同的代码(我的所有代码都在/lib而不是/js中)。

    我已经在FireBug中浏览了代码并检查了生成的源代码,它正在添加脚本标记,但是加载的函数上的断点永远不会触发。

    $(document).ready() 函数有一个简单的警报,但是FireBug给出了 $ is not defined 也就是说加载.js更新html文件时,浏览器(FireFox,但IE8表现出相同的行为)不会加载这些文件。

    更新: 我已经启用了“网络”选项卡。硬重新加载页面时(通过 ctrl键 + f5级 )共有9个请求,其中8个是304,一个是404(这是要加载的调用)徽标.png我从未复制过),其余的都是ColorBox css文件。列出的对象都不是应该通过加载.js正在加载的文件。所有的时间都是以毫秒为单位的,没有什么不寻常的。

    更新2:

    <!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" dir="ltr">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>yensdesign.com - How to create a stylish loading bar as Gmail in jQuery</title>
        <link rel="stylesheet" href="css/loading.css" type="text/css" media="screen" />
        <script src="lib/loading.js" type="text/javascript"></script>
    </head>
    <body onload="start()">
        <div id="restart">
    
            <a href="http://www.yensdesign.com"><img src="logo.jpg" alt="Go to yensdesign.com"/></a>
            <div id="button" onclick="restart()"><input type="submit" value="Restart me please!" /></div>
        </div>
        <div id="loadingZone">
            <div id="loadingSms">LOADING</div>
            <div id="infoProgress">0%</div>
            <br class="clear" />
            <div id="loadingBar">
    
                <div id="progressBar">&nbsp;</div>
            </div>
            <div id="infoLoading"></div>
        </div>
    </body>
    </html>
    

    注意名字的变化常规.css至加载.css. 这是加载.css除了名字的改变和常规.css:

    html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em,
    font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody,
     tfoot, thead, tr, th, td {
    border:0pt none;
    font-family:inherit;
    font-size:100%;
    font-style:inherit;
    font-weight:inherit;
    margin:0pt;
    padding:0pt;
    vertical-align:baseline;
    }
    body{
    background:#fff none repeat scroll 0%;
    font-size: 12px;
    font-family:tahoma,arial,sans-serif;
    margin:0pt;
    height:100%;
    }
    table {
    border-collapse:separate;
    border-spacing:0pt;
    }
    caption, th, td {
    font-weight:normal;
    text-align:left;
    }
    blockquote:before, blockquote:after, q:before, q:after {
    content:"";
    }
    blockquote, q {
    quotes:"" "";
    }
    a{
    cursor: pointer;
    text-decoration:none;
    }
    .clear{
    clear:both;
    }
    #button{
    text-align:center;
    margin:50px 50px 150px 50px;
    }
    #restart{
    display:none;
    text-align:center;
    }
    #loadingZone{
    margin:0 auto;
    width:410px;
    text-align:center;
    }
    #loadingBar{
    border:1px solid #c2c2c2;
    height:2px;
    text-align:left;
    line-height:0;
    margin:0;
    padding:0;
    overflow:hidden; /*fix for IE 6*/
    }
    #progressBar{
    height:2px;
    line-height:0;
    margin:0;
    padding:0;
    background:#b3f83d;
    width:0%;
    }
    #loadingSms{
    color:#6ea1fa;
    float:left;
    padding:10px 2px;
    }
    #infoProgress{
    color:#6ea1fa;
    float:right;
    padding:10px 2px;
    }
    #infoLoading{
    padding:10px;
    color:#b9b9b9;
    font-size:10px;
    }
    

    /***************************/
    //@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
    //@website: www.yensdesign.com
    //@email: yensamg@gmail.com
    //@license: Feel free to use it, but keep this credits please!                    
    /***************************/
    
    
    var LoadBar = function(){
        this.value = 0;
        this.sources = Array();
        this.sourcesDB = Array();
        this.totalFiles = 0;
        this.loadedFiles = 0;
    };
    //Show the loading bar interface
    LoadBar.prototype.show = function() {
        this.locate();
        document.getElementById("loadingZone").style.display = "block";
    };
    //Hide the loading bar interface
    LoadBar.prototype.hide = function() {
        document.getElementById("loadingZone").style.display = "none";
    };
    //Add all scripts to the DOM
    LoadBar.prototype.run = function(){
        this.show();
        var i;
        for (i=0; i<this.sourcesDB.length; i++){
            var source = this.sourcesDB[i];
            var head = document.getElementsByTagName("head")[0];
            var script = document.createElement("script");
            script.type = "text/javascript";
            script.src = source
            head.appendChild(script);
        }    
    };
    //Center in the screen remember it from old tutorials? ;)
    LoadBar.prototype.locate = function(){
        var loadingZone = document.getElementById("loadingZone");
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = loadingZone.clientHeight;
        var popupWidth = loadingZone.clientWidth;
        loadingZone.style.position = "absolute";
        loadingZone.style.top = parseInt(windowHeight/2-popupHeight/2) + "px";
        loadingZone.style.left = parseInt(windowWidth/2-popupWidth/2) + "px";
    };
    //Set the value position of the bar (Only 0-100 values are allowed)
    LoadBar.prototype.setValue = function(value){
        if(value >= 0 && value <= 100){
            document.getElementById("progressBar").style.width = value + "%";
            document.getElementById("infoProgress").innerHTML = parseInt(value) + "%";
        }
    };
    //Set the bottom text value
    LoadBar.prototype.setAction = function(action){
        document.getElementById("infoLoading").innerHTML = action;
    };
    //Add the specified script to the list
    LoadBar.prototype.addScript = function(source){
        this.totalFiles++;
        this.sources[source] = source;
        this.sourcesDB.push(source);
    };
    //Called when a script is loaded. Increment the progress value and check if all files are loaded
    LoadBar.prototype.loaded = function(file) {
        this.loadedFiles++;
        delete this.sources[file];
        var pc = (this.loadedFiles * 100) / this.totalFiles;
        this.setValue(pc);
        this.setAction(file + " loaded");
        //Are all files loaded?
        if(this.loadedFiles == this.totalFiles){
            setTimeout("myBar.hide()",300);
            //load the reset button to try one more time!
            document.getElementById("restart").style.display = "block";
        }
    };
    //Global var to reference from other scripts
    var myBar = new LoadBar();
    
    //Checking resize window to recenter :)
    window.onresize = function(){
        myBar.locate();
    };
    //Called on body load
    start = function() {
        myBar.addScript("lib/jquery-min.js");
        myBar.addScript("lib/jquery.colorbox-min.js");
        myBar.addScript("lib/jquery.pan-min.js");
        myBar.addScript("lib/raphael-min.js");
        myBar.addScript("lib/map.js");
        myBar.run();
    };
    //Called on click reset button
    restart = function(){
        window.location.reload();
    }; 
    

    关于如何让一个简单的在线脚本在localhost中工作有什么想法吗?或者类似的方法来执行在localhost中工作的加载屏幕?

    3 回复  |  直到 12 年前
        1
  •  2
  •   Telanor    15 年前

    script.src = "js/" + source . 另外,如果您的firebug net选项卡被禁用,请单击该选项卡,它旁边会显示一个箭头。单击该箭头并选择“启用”。

    编辑以响应更新:

    你说你已经发出警报了 $(document).ready

        2
  •  2
  •   SamStephens SQL.injection    15 年前

    在没有看到代码的情况下,我猜您正在从文件系统运行页面,javascript路径指向根目录,类似于/示例.js

    您确实应该使用Firebug的NET选项卡来诊断文件是否没有被加载,如果文件变灰,它被禁用,您应该能够在“NET”标题旁边的向下箭头上,并启用它。

        3
  •  0
  •   graham.reeds    15 年前

    抱歉浪费了你们的时间。