0

In my MVC project i import a .js file in my shared layout.

 <script src="@Url.Content("~/Scripts/bootbox.js")" type="text/javascript"></script>

This file has this variable

var locales = {
            en: {
                OK: "OK",
                CANCEL: "Nej",
                CONFIRM: "Ja"
            }}

In one of the views i would like to change the OK value for en with jQuery. How can i do this?

5
  • locales.en.OK = 'en';? Commented May 14, 2014 at 14:57
  • doesnt work, says its not defined Commented May 14, 2014 at 14:57
  • can we access js variable in one view to other? Commented May 14, 2014 at 14:58
  • Have you checked the scope of your JavaScript files? It sounds like the variable is out of scope where you're trying to access it. Commented May 14, 2014 at 15:03
  • @TobiasOlofsson Is the var defined within the body of a function? If it is, it won't be directly reachable from outside of that. Commented May 14, 2014 at 15:04

2 Answers 2

2

Assuming the file defines locales as a global variable,

locales.en.OK = 'en';

To be clear, though, this has nothing to do with jQuery, razor, or model-view-controller. It's simply how you set the property of an object in native Javascript, there's no further technology required to do this.


I have now downloaded bootbox.js myself to look at what's going on here. locales is not defined as a global variable, it's scoped to the anonymous wrapper function that contains all of the contents of bootbox.js.

This means that there is no way to change the contents of that variable dynamically from outside of that scope. If you want to permanently change that text, then you can change it in bootbox.js manually. If you want to change it dynamically, then you can replace

var locales = ...

with

window.locales = ...

in bootbox.js, and then use my original suggestion in your own code.

Sign up to request clarification or add additional context in comments.

Comments

0

You may declare locales in some scope, define locales outside of scope that is global or just remove var from locales as mentioned below.

locales = {
  en: {
    OK: "OK",
    CANCEL: "Nej",
    CONFIRM: "Ja"
}}

You can call it by locales.en.OK

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.