JavaScript 文件作为空白

标签 javascript html

不确定这里发生了什么。最初我在script.js中有一些代码但现在它已被重构并准备好放入自己的文件中。因此,在完成重构后,我将代码移至 base.js 。这是 html。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Cache-control" content="no-cache">
    <title>Draw</title>
    <link rel='stylesheet' type='text/css' href='style.css'/>
    <script src="libraries/jquery-2.1.4.js"></script>
</head>
<body>
<canvas id="canvas">
    HTML5 Canvas not supported.
</canvas>

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

但出于某种原因,base.js在 chrome 开发者工具中显示为空,并且 script.js提示它找不到 base.js 中定义的原型(prototype)。

我知道这个 URL 很好,因为我故意拼写错误,并且在开发者控制台中它向我大喊大叫,说在我更正拼写之前无法找到它。我知道它没有被缓存,因为我已经清除了缓存。最后,我尝试将名称更改为 base_prototype.js并且它有效。

为什么会发生这种情况?

编辑:添加 script.js base.js

//script.js
/**
 * Created by carlos on 5/20/15.
 */
var myBase;

$(function() {
    myBase = new Base(document.getElementById("canvas"));
    myBase.draw(minDim());



});

$( window ).resize(function() {
    myBase.draw(minDim());
});

function minDim(){
    return Math.min($(window).height(),$(window).width());

}

//base.js
/**
 * Created by carlos on 5/23/15.
 */
function Base(canvas){
    this.canvas = canvas;
    this.context = this.canvas.getContext("2d");
    this.grids = 40;
    this.canvasSize = 0;
    this.draw = function(size){
        this.canvasSize = size;
        this.canvas.width = size;
        this.canvas.height = size;
        $( this.canvas ).height(size);
        $( this.canvas ).width();

        this.drawZeroMatrixStockGrid(0,this.grids,0,this.grids);

        this.context.stroke();
    };

    this.drawZeroMatrixStockGrid = function (startX, endX, startY, endY){
        var adjust = this.grids/2;
        this.drawMatrixStockGrid(startX-adjust , endX-adjust , startY-adjust, endY-adjust);
    };
    this.drawMatrixStockGrid = function(startX, endX, startY, endY){

        this.context.beginPath();
        this.context.transform(.5, -.5, .5, .5, this.canvasSize/2, this.canvasSize/2);
        this.context.scale(1,1);
        for(var x = startX;x<endX + 1 ;x++){
            this.context.moveTo(this.getXCoord(this.canvasSize,this.grids,x),this.getYCoord(this.canvasSize,this.grids,startY));
            this.context.lineTo(this.getXCoord(this.canvasSize,this.grids,x),this.getYCoord(this.canvasSize,this.grids,endY));
        }

        for(var y = startY;y<endY + 1;y++){
            this.context.moveTo(this.getXCoord(this.canvasSize,this.grids,startX),this.getYCoord(this.canvasSize,this.grids,y));
            this.context.lineTo(this.getXCoord(this.canvasSize,this.grids,endX),this.getYCoord(this.canvasSize,this.grids,y));
        }

    };


    /**
     * assumes a square canvas
     * TODO use global vars to clean signature
     * TODO memoize
     * */
    this.getXCoord = function (canvasSize, grids, coord){
        var delta = canvasSize/(grids);
        return delta*coord;
    };
    this.getYCoord = function(canvasSize, grids, coord){
        var delta = canvasSize/(grids);
        return delta*coord;
    };


}

最佳答案

如果您可以通过 file://但不能通过 http://在本地查看文件,则问题很可能来自服务器设置。响应处理程序可以在没有警告的情况下覆盖您的静态文件并发送成功的状态调用。

正如您通过我们的评论发现的那样,REST Controller 确实将 GET 请求映射到“[/base]”模式,覆盖与 Assets 名称匹配的静态文件的任何尝试。更改文件名是解决问题的最快途径,至少在前端是这样。

干杯!

关于JavaScript 文件作为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30425416/

相关文章:

javascript - 在类中完成的自定义事件

javascript - 单击“显示”按钮后如何获取值文本作为结果按钮的结果?

html - Outlook 2010 正在调整 HTML 电子邮件而非图像的大小

html - pull-right bootstrap 改变显示的 div 顺序

html - 如何在 CSS 中将元素固定在顶部?

html - Bootstrap 垂直对齐与列环绕和不同高度

html - 使用 'ß' 时,Internet Explorer 不会将 'SS' (ß/es-zed) 更改为 'text-transform: uppercase;'

javascript - iPhone 不支持内联视频 - 是否可以使用 JS 从上传的视频文件中去除帧和音频?

JavaScript:替换数组中的字符串

javascript - 将输入字段绑定(bind)到字典