简体   繁体   English

在ASP.NET MVC 3中引用自定义外部javascript文件

[英]Referencing a custom external javascript file in asp.net mvc 3

Hi I'm trying to pull some javascript on a page off of the page and putting it into an external javascript file for reusability. 嗨,我正在尝试从页面上拉出页面上的一些javascript,并将其放入外部javascript文件中以实现可重用性。

Just as a test I'm just trying to display an alert text box but even that's not working. 作为测试,我只是试图显示一个警报文本框,但即使这样也不起作用。

Here's what I have so far. 到目前为止,这就是我所拥有的。

Page with the javascript 带有javascript的页面

EDIT: Ignore the type="text/javascript" that wasn't the problem 编辑:忽略不是问题的type =“ text / javascript”

在此处输入图片说明

Here's the actual content of the script.js 这是script.js的实际内容

在此处输入图片说明

File Structure from the site root 网站根目录中的文件结构

在此处输入图片说明

Honestly it looks like it should work but whenever I run the page in the browser I'm getting this error. 老实说,它似乎应该可以工作,但是每当我在浏览器中运行页面时,我都会收到此错误。

在此处输入图片说明

I know that the path is correct, but what am I missing? 我知道路径是正确的,但是我缺少什么呢?

问题是我没有清除缓存,对不起新手错误:(

Looks like some simple javascript syntax errors; 看起来像一些简单的javascript语法错误; missing semicolons. 缺少分号。 You can use JSLint to find those. 您可以使用JSLint来找到那些。 This should work (Assuming that jQuery is loading before your other two scripts): 这应该可以工作(假设jQuery在其他两个脚本之前加载):

Your 'script.js': 您的“ script.js”:

var projectApi = function() {};

projectApi.prototype.HelloWorld = function() {
    alert('Hello World');
};

Your page javascript: 您的页面javascript:

$(document).ready(function() {
    var api = new projectApi();
    api.HelloWorld();
});

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

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