Localization Helper, How to retrieve values from javascript? - javascript

I used localization helper from Matt Hawley. It's really working great. I have a problem though in getting the values on javascripts/jquery.
I can't retrieve the resources text using this:
example: alert('<%=Html.Resource(\"Strings,SomeKey\")%>');
Any help is greatly appreciated.
Best

Thank you BalusC for the reply. Greatly appreciated. I already got a work around. I declared variable on my .aspx page with the resources key values. Then call the variable on my separate js. So I have it like this:
In my aspx page.
<script>
var foo="<%=Html.Resource("Strings,SomeKey")%>";
</script>
In my js. I just simply called the variable foo.
alert(foo);
There I have it!:)
I got it wrong for the first time since my variable was declared on my external js. Therefore asp.net will not be able to interpret it. Instead it will be rendered as plain text.
Best regards

Related

How to access environment variable from rails on your javascript?

I am trying to set up a key system in the application I'm building. I have a code that has this logic:
$('.key-test-button').on('click', function(){
if ($('.key-test-input').val() === "MYKEYHERE"){
$('#hidden-div-instructor').show();
}
});
And My Idea was to replace "MYKEYHERE" for a hidden value on my code that I would store as a environment variable. I know how to do that for APIs for example, inside rails, but Im not familiar how I could access this variable inside my javascript/jquery script. I would love if someone could give me a insight. Thanks you.
As #davidhoelzer mentioned, you can't do this with pure js (as far as I know)... you could, however, change the .js file to .js.erb and use ruby to access/manipulate your environment variable that way.

Using gsp layout declared variable in javaScript file that is being loaded in the same page

I recently started learning grails and I am trying to use a gsp variable declared in the layout of the page as:
<g:set var="abtestType" value="newSearchBar" />
in the js file that is being loaded on the same page. Things that I have tried:
alert(${abtestType});
alert(<%=abtestType%>);
alert("abtestType:"+abtestType);
but its showing error as variable is not defined. Probably I am trying to fetch it in wrong way, need help regarding this.
Even thinking about doing so neither makes sense nor applicable.
Reason for such statement is that when a gsp page is rendered to an html page, it replace grails tags with appropriate html tags or value. Similarly it replaces the ${} or <%%> with html or javascript or whatever that goes on front-end.
Hence the code that you have tried could have worked fine if you were having those javascript code lines in the gsp itself but as you have called externalised js file it actually don't know anything about gsp or jsp or any other Language's front-end support.
The one way of doing that if using global variable in javascript. e.g.
declare abtestType above like below:
<script>
var abtestType = "${abtestType}"
</script>
Now you have access to global variable abtestType in your javascript.
Use it in your javascript but remember now you need to have this variable iff the code using it is called otherwise very same error would you get i.e. variable is not defined
There is another way that I found in this post but is a manipulation actually.
Is there any analogue in Javascript to the __FILE__ variable in PHP?
Also, another good links is
Pass vars to JavaScript via the SRC attribute
Hope it helps!

how to get requestscope variable in external javascript

I have some problem regarding request scope variables in spring application. Problem is I have an Object "xyz" which was added to reference data map in SimpleFormController. Now I want to get this object(xyz) from java script. This is done in internal javascript as '${xyz}', but I need to get this from external javascript file. please any one help me out. I know external java script file not come under request scope, But is there any possible solution?
thanks in advance,
The answer is, declare the script variable at global level. Now that variable can have the scope to external scripts files also.

Rails 3 and jQuery- how to transmit ruby variable to javascript

I would like to ask you - I have javascript code and I need into this part of code transmit variable with data from database. This variable is in controller. How can I do it?
I tried something like (in *.js):
$("#div").append('<%= escape_javascript(#test) %>hhhhhh');
or
$("#div").append('<%= #test %>hhhhhh');
But the codes above not works... Could you help me, please, how to do?
Thank you, Manny
If you are rendering a response to AJAX-call, that code should go in your_action.js.erb file. Otherwise, if it's just inline JavaScript, place it in your_action.html.erb under app/views/your_controller directory.
Correct me if I understood you wrong.

Accessing Django template {{Variable}} from JavaScript

I tried to access django template variable in html page inline javascript, it works fine.
But if I include js using <script src="..> then it dont work.
Is this limitation or I'm doing something wrong?
I really appreciate your help.
The included Javascript isn't processed by the Django template processor on the server, so that won't work. If you need to pass information through the template to included Javascript files, have your template create a small <script> block wherein some global variable is declared to contain those template variables. Then, your pure Javascript file can get the values by looking for the global object created by that <script> from the template.
Pointy's answer is correct. I often find this filter useful for that situation:
#register.filter(name='json')
def _json(obj):
#remember to make sure the contents are actually safe before you use this filter!
return safestring.mark_safe(json.dumps(obj))
then in a <script> tag I can just do something like this:
window.g_details = {{ details|json }};

Categories

Resources