In my MVC application, I am faced with the challenge of reading values from a resource file and passing them to a JavaScript file. Currently, I am calling a separate JavaScript function from a cshtml file, which in turn passes parameters containing the resource file values to another function within the same namespace. However, I am struggling to access these resource file values within this second JavaScript function. Can anyone provide guidance on how to achieve this?
This is what my cshtml code looks like:
<script type="text/javascript">
Mynamespace.function2("@Globalstrings.Value1","@Globalstrings.Value2");
</script>
Here is my JS file:
var Mynamespace ={
function1: function(){
Mynamespace.function2.getMess(); // I need to access value1 here in this function
},
function2: function(message1,message2){
var value1 = message1;
var value2 = message2;
return{
getMess: function(){ return value1;},
getLab: function() { return value2;}
}
}
}
Despite implementing the above code, I am encountering the issue of getting 'undefined' in the getMess function. Hence, I am open to two potential solutions for this problem:
- Is there an alternative method for retrieving resource file values in JavaScript?
- How can we rectify the existing JavaScript code mentioned above?
I posted a similar query earlier today (Accessing variable in another function, returns undefined - JavaScript), however, I realized that my initial question was too focused on a JavaScript scoping issue only. This post aims to address the actual problem at hand.
Your assistance in resolving this matter would be highly appreciated.