Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6534953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:18:48+00:00 2026-05-25T10:18:48+00:00

I have tried many different ways to organize the JavaScript that is specific for

  • 0

I have tried many different ways to organize the JavaScript that is specific for each View now, but I have not found any way that I feel comfortable with. It seems messy anyway. Probably (and hopefully) that is because I haven’t been working with JavaScript very long, and there is a good way of doing this.

Currently what I’m doing is this:

In my Layout file, I have the a RenderSection for scripts in addition to RenderBody. This section contains all JavaScript relevant for each single view. The global script is tucked away in it’s own file.

Inside this sections there is a lot of different part of JavaScript (for my biggest View currently there is about 600 lines of JavaScript):

  • Some definitions of variables and setting different settings (jQuery settings among others).
  • Hiding different DOM elements on the screen that will be made visible when the user interacts with the View later on.
  • A lot of jQuery code for different events linked to DOM elements ( click/keyup++ )
  • Some code refactored into methods because they are used by different jQuery events.

The things I don’t like here are mainly two things:

  • All this code is put into one big codeblock and it’s hard to find the parts of the script I’m looking for. Basically, it gets quite unmaintainable as the script grows.
  • The script is located in the same file as the View. I would like to put the script into a seperate file, but I can’t since I use different parts of my Model, and some HtmlHelpers inside the script as well. For example, where I execute some $.post calls, I use @Url.Action('...') to link it to the correct action method to be sure that my script will continue to work even if I change my routing.
    I also use the Model to decide if some elements should start out hidden or not like this (is this an ok way to make it start out hidden, or is there a better way? It seems kind of hacky even if I can’t put my finger on it).:

code

@if( Model.SomeBoolValue ){
  @:$("#DOMelementID").hide();
}

Some pointers to get me in the right direction would be highly appreciated. I need to get this better structured before I lose control over my own code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:18:49+00:00Added an answer on May 25, 2026 at 10:18 am

    I would recommend you taking a look at jQuery plugin authoring and organize your javascript code in terms of plugins that you will attach to your DOM elements.

    As far as the @Url.Action('...') problem is concerned there are many ways to solve this issue and externalize your scripts into separate files. For example let’s suppose that you are AJAXifying an existing form or ActionLink which already contains the url:

    $('#myAnchor').click(function() {
        // use this.href to fetch the url
        $.post(this.href, function(result) {
    
        });
        return false;
    });
    

    Now if you simply wanted to send an AJAX request when a user clicks on a div for example, you could always use HTML5 data-* attributes (the same way ASP.NET MVC 3 unobtrusive AJAX and validation works) to define this url on the DOM element:

    <div id="mydiv" data-url="@Url.Action("Some Action")">click me</div>
    

    and now in a separate file

    $('#mydiv').click(function() {
        var url = $(this).data('url');
        $.post(url, function(result) {
    
        });
    });
    

    and if you follow my first advice about writing a jQuery plugin your code will look like this:

    $('#mydiv').myplugin();
    

    Now let’s consider the following snippet:

    @if(Model.SomeBoolValue) {
        @:$("#DOMelementID").hide();
    }
    

    So from what it seems here you are using the view model properties to show/hide sections of your code. OK, here’s what I may suggest you: JSON serialize this model into the view and then you can start passing values from it to your newly developed jQuery plugins:

    <script type="text/javascript">
         var model = @Html.Raw(Json.Serialize(Model));
         // now configure configure your plugins, for example
         $('#mysection').myplugin({ model: model });
    </script>
    

    Now inside your plugin you will have access to absolutely all properties of your view model.

    And that’s all that you will need to put in your view. All the other javascript will be of course in separate javascript files, properly organized in reusable jQuery plugins.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this must be a trivial question, but I've tried many different ways,
I've tried many different ways of accessing the name of an attribute, but just
Hello I have tried this many different ways and have followed the tutorial at
I have tried different examples to filter a gridview by dropdownlist, but is it
I have now tried to get this to work for a few hours but
Over the years, I have tried many times to find a good, easy to
I have tried what seems like everything - I've done similiar things many times
I have tried this... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item(Post), \b\#\b) But it is
I have tried to integrate the Picasa API on iPhone, compiles fine, but I
I have tried to find how to create DLL-s on linux using google, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.