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

  • SEARCH
  • Home
  • 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 281101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:10:29+00:00 2026-05-12T05:10:29+00:00

I want JavaScript code to be separated from views. I got the requirement to

  • 0

I want JavaScript code to be separated from views.
I got the requirement to implement localization for a simple image button generated by JavaScript:

<img src="..." onclick="..." title="Close" />

What’s the best technique to localize the title of it?

PS: I found a solution by Ayende. This is the right direction.

Edit:
I got Localization helper class which provides the Controller.Resource('foo') extension method.

I am thinking about to extend it (helper) so it could return all JavaScript resources (from “ClientSideResources” subfolder in App_LocalResources) for the specified controller by its name. Then – call it in BaseController, add it to ViewData and render it in Layout.

Would that be a good idea?

  • 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-12T05:10:29+00:00Added an answer on May 12, 2026 at 5:10 am

    EDIT

    Consider writing the necessary localized resources to a JavaScript object (hash) and then using it for lookup for your dynamically created objects. I think this is better than going back to the server for translations. This is similar to adding it via viewdata, but may be a little more flexible. FWIW, I could consider the localization resources to be part of the View, not part of the controller.

    In the View:

    <script type="text/javascript"
             src='<%= Url.Content( "~/Resources/Load?translate=Close,Open" %>'></script>
    

    which would output something like:

    var local = {};
    local.Close = "Close";
    local.Open = "Open";
    

    Without arguments it would output the entire translation hash. Using arguments gives you the ability to customize it per view.

    You would then use it in your JavaScript files like:

     $(function(){
         $('#button').click( function() {
            $("<img src=... title='" + local.Close + "' />")
               .appendTo("#someDiv")
               .click( function() { ... } );
         });
     });
    

    Actually, I’m not too fussed about keeping my JavaScript code out of my views as long as the JavaScript code is localized in a container. Typically I’ll set my master page up with 4 content area: title, header, main, and scripts. Title, header, and main go where you would expect and the scripts area goes at the bottom of the body.

    I put all my JavaScript includes, including any for viewusercontrols, into the scripts container. View-specific JavaScript code comes after the includes. I refactor shared code back to scripts as needed. I’ve thought about using a controller method to collate script includes, that is, include multiple scripts using a single request, but haven’t gotten around to that, yet.

    This has the advantage of keeping the JavaScript code separate for readability, but also allows me to easily inject model or view data into the JavaScript code as needed.

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

Sidebar

Ask A Question

Stats

  • Questions 205k
  • Answers 205k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer this is not a stupid question. However, I believe that… May 12, 2026 at 8:54 pm
  • Editorial Team
    Editorial Team added an answer You probably have strings instead of integers. That is your… May 12, 2026 at 8:54 pm
  • Editorial Team
    Editorial Team added an answer you need to iterate your listbox items like.. foreach (ListItem… May 12, 2026 at 8:54 pm

Related Questions

I have a bunch of javascript functions which depend on some server-side constants (such
There are many examples on the internet of doing this type of thing. But
Let's say I have cleaner like this .cleaner:after { content: '.'; display: block; clear:
I want to have a List or Array of some sort, storing this information

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.