Javascript 如何引用本地js文件?

Javascript 如何引用本地js文件?,javascript,jquery,Javascript,Jquery,我有一个js文件,无法在本地引用 如果我在线部署它并从那里引用它,它就会工作 <script src="http://acwebsite.azurewebsites.net/scripts/table2excel.js"></script> 但是如果我复制文件中的文本并引用本地js文件,它将无法读取它 <script src ="~/Scripts/table2excel.js"></script> 不知道该怎么办。 这是js文件: /

我有一个js文件,无法在本地引用

如果我在线部署它并从那里引用它,它就会工作

<script src="http://acwebsite.azurewebsites.net/scripts/table2excel.js"></script>

但是如果我复制文件中的文本并引用本地js文件,它将无法读取它

<script src ="~/Scripts/table2excel.js"></script>

不知道该怎么办。 这是js文件:

//table2excel.js
; (function ($, window, document, undefined) {
    var pluginName = "table2excel",

    defaults = {
        exclude: ".noExl",
        name: "Table2Excel"
    };

    // The actual plugin constructor
    function Plugin(element, options) {
        this.element = element;
        // jQuery has an extend method which merges the contents of two or
        // more objects, storing the result in the first object. The first object
        // is generally empty as we don't want to alter the default options for
        // future instances of the plugin
        //
        this.settings = $.extend({}, defaults, options);
        this._defaults = defaults;
        this._name = pluginName;
        this.init();
    }

    Plugin.prototype = {
        init: function () {
            var e = this;

            var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">";
            e.template = {
                head: "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">" + utf8Heading + "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",
                sheet: {
                    head: "<x:ExcelWorksheet><x:Name>",
                    tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
                },
                mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",
                table: {
                    head: "<table>",
                    tail: "</table>"
                },
                foot: "</body></html>"
            };

            e.tableRows = [];

            // get contents of table except for exclude
            $(e.element).each(function (i, o) {
                var tempRows = "";
                $(o).find("tr").not(e.settings.exclude).each(function (i, o) {
                    tempRows += "<tr>" + $(o).html() + "</tr>";
                });
                e.tableRows.push(tempRows);
            });

            e.tableToExcel(e.tableRows, e.settings.name, e.settings.sheetName);
        },

        tableToExcel: function (table, name, sheetName) {
            var e = this, fullTemplate = "", i, link, a;

            e.uri = "data:application/vnd.ms-excel;base64,";
            e.base64 = function (s) {
                return window.btoa(unescape(encodeURIComponent(s)));
            };
            e.format = function (s, c) {
                return s.replace(/{(\w+)}/g, function (m, p) {
                    return c[p];
                });
            };

            sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName;

            e.ctx = {
                worksheet: name || "Worksheet",
                table: table,
                sheetName: sheetName,
            };

            fullTemplate = e.template.head;

            if ($.isArray(table)) {
                for (i in table) {
                    //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;
                    fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail;
                }
            }

            fullTemplate += e.template.mid;

            if ($.isArray(table)) {
                for (i in table) {
                    fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
                }
            }

            fullTemplate += e.template.foot;

            for (i in table) {
                e.ctx["table" + i] = table[i];
            }
            delete e.ctx.table;

            if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
            {
                if (typeof Blob !== "undefined") {
                    //use blobs if we can
                    fullTemplate = [fullTemplate];
                    //convert to array
                    var blob1 = new Blob(fullTemplate, { type: "text/html" });
                    window.navigator.msSaveBlob(blob1, getFileName(e.settings));
                } else {
                    //otherwise use the iframe and save
                    //requires a blank iframe on page called txtArea1
                    txtArea1.document.open("text/html", "replace");
                    txtArea1.document.write(e.format(fullTemplate, e.ctx));
                    txtArea1.document.close();
                    txtArea1.focus();
                    sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings));
                }

            } else {
                link = e.uri + e.base64(e.format(fullTemplate, e.ctx));
                a = document.createElement("a");
                a.download = getFileName(e.settings);
                a.href = link;

                document.body.appendChild(a);

                a.click();

                document.body.removeChild(a);
            }

            return true;
        }
    };

    function getFileName(settings) {
        return (settings.filename ? settings.filename : "table2excel") +
               (settings.fileext ? settings.fileext : ".xlsx");
    }

    $.fn[pluginName] = function (options) {
        var e = this;
        e.each(function () {
            if (!$.data(e, "plugin_" + pluginName)) {
                $.data(e, "plugin_" + pluginName, new Plugin(this, options));
            }
        });

        // chain jQuery functions
        return e;
    };
})(jQuery, window, document);
//table2excel.js
; (函数($,窗口,文档,未定义){
var pluginName=“table2excel”,
默认值={
排除:“.noExl”,
名称:“Table2Excel”
};
//实际的插件构造函数
函数插件(元素、选项){
this.element=元素;
//jQuery有一个extend方法,它合并两个或多个数据库的内容
//更多对象,将结果存储在第一个对象中。第一个对象
//通常为空,因为我们不想更改的默认选项
//插件的未来实例
//
this.settings=$.extend({},默认值,选项);
这是默认值;
这个。_name=pluginName;
this.init();
}
Plugin.prototype={
init:函数(){
var e=此;
var utf8Heading=“”;
e、 模板={
标题:“+utf8Heading+”,
表:{
头:“,
尾巴:“
},
脚:“
};
e、 tableRows=[];
//获取表的内容(排除除外)
$(e.element)。每个(函数(i,o){
var tempRows=“”;
$(o).find(“tr”).not(e.settings.exclude).each(函数(i,o){
tempRows+=“”+$(o).html()+“”;
});
e、 tableRows.push(tempRows);
});
e、 tableToExcel(e.tableRows、e.settings.name、e.settings.sheetName);
},
tableToExcel:函数(表、名称、图纸名称){
var e=此,fullTemplate=“”,i,link,a;
e、 uri=“data:application/vnd.ms excel;base64”;
e、 base64=函数{
返回窗口.btoa(unescape(encodeURIComponent));
};
e、 格式=函数(s,c){
返回s.replace(/{(\w+)}/g,函数(m,p){
返回c[p];
});
};
sheetName=typeof sheetName==“未定义”?“工作表”:工作表名称;
e、 ctx={
工作表:名称| |“工作表”,
表:表,,
sheetName:sheetName,
};
fullTemplate=e.template.head;
如果($.isArray(表)){
对于(表中的i){
//fullTemplate+=e.template.sheet.head+“{sheet”+i+“}”+e.template.sheet.tail;
fullTemplate+=e.template.sheet.head+sheetName+i+e.template.sheet.tail;
}
}
fullTemplate+=e.template.mid;
如果($.isArray(表)){
对于(表中的i){
fullTemplate+=e.template.table.head+“{table”+i+“}”+e.template.table.tail;
}
}
fullTemplate+=e.template.foot;
对于(表中的i){
e、 ctx[“表”+i]=表[i];
}
删除e.ctx.table;
如果(msie的类型!=“未定义”&&msie>0 | |!!navigator.userAgent.match(/Trident.*rv \:11\。/)//如果Internet Explorer
{
如果(Blob的类型!=“未定义”){
//如果可以的话,使用blob
fullTemplate=[fullTemplate];
//转换为数组
var blob1=new Blob(fullTemplate,{type:“text/html”});
msSaveBlob(blob1,getFileName(e.settings));
}否则{
//否则,请使用iframe并保存
//需要名为txtArea1的页面上的空白iframe
txtArea1.document.open(“text/html”、“replace”);
txtArea1.document.write(e.format(fullTemplate,e.ctx));
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand(“SaveAs”,true,getFileName(e.settings));
}
}否则{
link=e.uri+e.base64(e.format(fullTemplate,e.ctx));
a=document.createElement(“a”);
a、 下载=获取文件名(如设置);
a、 href=链接;
文件.正文.附件(a);
a、 单击();
文件.body.removeChild(a);
}
返回true;
}
};
函数getFileName(设置){
返回(settings.filename?settings.filename:“table2excel”)+
(settings.fileext?settings.fileext:“.xlsx”);
}
$.fn[pluginName]=函数(选项){
var e=此;
e、 每个(函数){
if(!$.data(e,“plugin_”+pluginName)){
$.data(e,“plugin_”+pluginName,新插件(此,选项));
}
});
//链jQuery函数
返回e;
};
})(jQuery、窗口、文档);

如果您使用的是相对路径,请使用下面的方法

src ="./Scripts/table2excel.js"

“无法读取”是什么意思?删除您的
~
您应该提供js文件的本地路径我确信您指定的js文件的路径不正确。您的目录/文件结构是什么?