1

i have a set of aspx page & its associated controller. now i want to add a js file for a aspx page. i'm not getting how to add and link to particular aspx page in MVC.

what is the easiest way to add and link for the particular aspx page.

Edit

i created a folder inside Scripts-->Views-->Controllername-->somejsfile.js file.

in site master, below the jquery scripts loading place, i added

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

Now i have added init function inside js like

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

4
  • possible duplicate of How to include js files in asp.net MVC and have a valid path on all routes Commented Apr 23, 2014 at 5:48
  • But how do i link to a perticualar aspx page, lets take i have 5 aspx page with name index.aspx. Now i want to attach js file for a single aspx out of 5. how to do? Commented Apr 23, 2014 at 5:52
  • In mvc you will not directly link to aspx page instead you have to create link for the route. Your question is still not clear. Please edit it and describe your problem well. Commented Apr 23, 2014 at 5:56
  • i have edited, please have a look Commented Apr 23, 2014 at 6:03

1 Answer 1

2

It is that simple, in your master file add a <script> block and use the jquery $(document).ready() function to init the script. (Assuming you are using jquery)

<script>
$(document).ready(function() {
    somejsfileNamescript.Init();
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.