Grails <g:set tag in javascript - javascript

how to set grails variable in javascript?
I also tried
<a onclick="myFunction(${aCase.id})" .../>
...
<script>
function myFunction(id) {
<g:set var="caseId" value="id"/>
}
</script>
value of caseId always 'id' (string), i want it value of 'aCase.id'
thanks!
(sorry, i not good at english)

You can not set a gsp var with javascript. Cause the gsp var is set before the gsp is rendered on server side, and the javascript runs on client side.

<script>caseId = ${id}</script>

Related

How to pass or use variable from GSP in Javascript

I have below line in my grails gsp file.
<div class="pagination">
<g:paginate total="${lotCount ?: 0}" />
</div>
I want to pass lotCount value to one of my javascript named area-switcher.js file to further use it. How can I do this?
I tried to refer one suggestion from How to pass a value directly from a controller to a javascript file in Grails
where I do below in my gsp
<g:javascript> var theId = ${lotCount} </g:javascript>
and try below in my js file for testing
alert(theId);
but it doesn't work.
Got error like ReferenceError: theId is not defined.
Please help.
Use a hiddenField:
<g:hiddenField name="lotCount" value="${lotCount}" />
<div class="pagination">
<g:paginate total="${lotCount ?: 0}" />
</div>
Your solution should work.
Just ensure that in source of generated html page line
<g:javascript> var theId = ${lotCount} </g:javascript>
is placed before line which includes area-switcher.js
<script type="text/javascript" src="area-switcher.js">
Besides that, there are two more options to pass some value from .gsp to javascript file (examples use jQuery):
By using of data attribute. For example,
gsp (here div, span, input, other tags could be used):
<div id="countElem" data-count="${count}">
js(jQuery used here):
var count = $("#countElem").data('count');
As was mentioned in another comments, by using of hidden field:
gsp:
<g:hiddenField name="countElem" data-count="${count}"/>
js(jQuery used here):
// hidden field will have name and id equals to countElem
var count = $("#countElem").val();

Interpret variable in Javascript file Laravel

I need to include a js file in my views.
But in this js file i need to interpret somes PHP variable.
Actually i do this :
#section('javascript')
<script>
alert("{{{test}}}");
</script>
#stop
But i REALLY need to do this :
#section('javascript')
{!! Html::script('js/test.js') !!}
#stop
test.js :
alert("{{{test}}}");
I need to declare a lot o variable. So my JS file will be very huge. And i don't want to show this directly in the source code.
How can i do ?
Thank you !
You can only pass the variable to the javascript like so:
#section('javascript')
<script>
var test = {{{$test}}};
</script>
#stop
then in your javascript file included at the bottom you can use it:
alert(test);
Let me just mention that this is not a great way of handling the passing variables from php to javascript.
When I need to do something like this, I usually create a meta tag on the page which would contain the alert information.
<meta name="someAlertValue" content="{{{ $test }}}" />
Then you can very easily grab that via jQuery.
var alert_text = $('meta[name=someAlertValue]').attr('content');
I find this approach to be much cleaner and maintainable than trying to drop php variables directly into your javascript.
I had the same problem, and wanted to have a stand alone js which will have bunch of variables taken from config() or even from database, multi-language, and will be configurable, or even will work with query parameters.
Maybe it's a hard way, but i've created a route:
Route::get('/js-modules/test.js',function(){ return view('js-modules.test');})->name('my_js);
So in the view resources/views/js-modules/test.blade.php you can put your js code together with your PHP stuff.
Or you can route it to a controller and have even more background work. it looks a bit slow (in the browser) on the first run , but second request it'll be cashed and retrieved as the regular js file
And now you can link it to any of your pages with
<script src="{{route('my_js')}}"></script>

how to access variable in javascript

I'm using javascript in my html page.
I've defined a variable 'language' in html:
<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
and I would like to use/modify this variable from javascript.
function changeLanguage(name) {
language = "fr";
}
how can I do it ?
What I have tried
I have a picture in html page and after clicking on it I call this js function. when it's called, variable 'language' should be changed to 'fr' . Maybe it can be done easier. I dont know.
Here JSTL code is executed at server side and the server sees the JavaScript/Html codes as simple texts. The generated contents from JSTL code (if any) will be rendered in the resulting HTML along with your other JavaScript/HTML codes. Now the browser renders HTML along with executing the Javascript codes. Now remember there is no JSTL code available for the browser.
So you cant use javascript to change value.
Change attributes(var) value(language) using JavaScript
Place this in your HTML Page
HTML
<c:set id="lang" var="language" scope="session" value="...." scope="...." />
<img src="abc.png" onclick="changelang()" />
<script>
$(document).ready(function(){
function changelang(){
$("#lang").attr("var", "fr");
}
});
</script>
Credit:
http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_dom_attr_set

ASP.net MVC insert JavaScript function result in view

I'm trying to do the following thing: <div>javascript:processData(Model.Property)</div>, where "processData" is a function that returns a string. How can this be done?
(This obviously is not working, it just returns the string "javascript:processData(Model.Property)".)
You can call the javascript method on jquery document ready and set the result to that div using html method.
#model YourSomeModelWithThatProperty
<div id="thatDiv"></div>
<script type="text/javascript">
$(function(){
var thatDataString=processData("#Model.Property");
$("#thatDiv").html(thatDataString);
});
</script>
Read the following question :
Using Razor within JavaScript
You need razor text markers:
http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx
<script type="text/javascript">
function processData(property) {
// all kinds of stuff...
// return result;
}
</script>
...
<input type="button" onClick="return processData(<%: Model.Property %>); />
If you're using Razor syntax, replace <%: Model.Property %> with #Model.Property.
As for displaying the result, use the javascript to put the value somewhere on the page (such as the text of an <a> element or an alert).

How do I use Javascript in my Coldfusion page?

I have a coldfusion page and I am very newbie in coldfusion. What I need to do is to insert the alert in between to see what is the time. In php I could close the php tags and enter the javascript tag and alert out the value. How would I do that in coldfusion? I have this
<cfset right_now=Now()>
<cfscript>
alert(#right_now#);
</cfscript>
But its not working.
thanks
<cfscript> is a Coldfusion tag for using the Coldfusion scripting language (aka CFScript). If you want to use Javascript, open a <script> tag like you would normally in HTML. You'll probably want to make sure it's inside a <cfoutput> tag if you want to use Coldfusion values within your javascript.
<cfset right_now = Now()>
<cfoutput>
<script type="text/javascript">
alert('#right_now#'); // don't forget you need to put quotes around strings in JS
</script>
</cfoutput>
You don't need to even use cfscript for this specific need. You could, for instance, do this:
<script type="text/javascript">
var currtime = new Date();
alert(currtime);
</script>
... Also a point to remember, you can't directly output HTML from within a <cfscript> tag. You can however get around this by calling a function from within a <cfscript> tag that can output the data for you.
Always remember the coldfusion begins and ends before anything else is executed: html, javaScript, sql, etc., so the javascript is getting an already formed code, which is CF instead of being hard coded.

Categories

Resources