简体   繁体   English

如何在MVC中为Aspx页面添加Js文件

[英]How to add Js file for a aspx page in MVC

i have a set of aspx page & its associated controller. 我有一组aspx页面及其关联的控制器。 now i want to add a js file for a aspx page. 现在我想为aspx页面添加一个js文件。 i'm not getting how to add and link to particular aspx page in MVC. 我没有得到如何添加和链接到MVC中的特定aspx页面。

what is the easiest way to add and link for the particular aspx page. 为特定的aspx页添加和链接的最简单方法是什么。

Edit 编辑

i created a folder inside Scripts-->Views-->Controllername-->somejsfile.js file. 我在脚本->视图->控制器名称-> somejsfile.js文件中创建了一个文件夹。

in site master, below the jquery scripts loading place, i added 在网站管理员中,jquery脚本加载位置下方,我添加了

<script src="/Scripts/Views/ControllerName/somejsfile.js?Version=<%=ConfigurationManager.AppSettings["ScriptVersion"]%>" type="text/javascript"></script>

Now i have added init function inside js like 现在我在js中添加了init函数,例如

var somejsfileNamescript= function () {
    return {

        Init: function () {

            alert("Hello");

        }
    }
} ();

I want this to get executed when the page is loaded. 我希望在页面加载时执行此操作。 This is the only one js file present in my application thanks 这是我的应用程序中存在的唯一一个js文件,谢谢

It is that simple, in your master file add a <script> block and use the jquery $(document).ready() function to init the script. 就是这么简单,在主文件中添加一个<script>块,然后使用jquery $(document).ready()函数初始化脚本。 (Assuming you are using jquery) (假设您正在使用jQuery)

<script>
$(document).ready(function() {
    somejsfileNamescript.Init();
});
</script>

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

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