I am working on a MVC application and I need to read resource file and get values , pass them in a javascript file. For this I am just calling a js function written in separate js file, from cshtml file. This function passes parameters which have the resource file value, to the function in javascript. Now in js file, I have another function in the same namespace as my above function where I need to read these resource file values. How can I do that?
My cshtml code:
<script type="text/javascript">
Mynamespace.function2("@Globalstrings.Value1","@Globalstrings.Value2");
</script>
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;}
}
}
}
When I tried the above, I am getting undefined in getMess. So, I could have 2 options primarily which can be accepted as an answer for this:
- Any other way of reading resource file value in js?
- Any fix for the above js code
I actually posted this question few hours ago (Accessing variable in another function, returns undefined - JavaScript), but made it limited to javascript scoping problem only,but then realised I will post the actual problem.
Any help would be appreciated.
You need a comma in your return object for setvalues. It's still not going to work, but at least that fixes your syntax error.
Also, it seems kind of odd returning the value1 and value2 in
setvalue()
, could I suggest returning the values in the getter? Fixed and refactored in this Fiddle