Thymeleaf inline javascript expression with variable - javascript

I'm using Spring Boot with Thymeleaf, and know I can use variables from my controller on the following way in javascript:
<script th:inline="javascript">
/*<![CDATA[*/
var username = [[${#authentication.principal.person.isSubscribedTo("random string")}]];
/*]]>*/
</script>
Now I tried to use a local variable outside the CDATA comment like this. I expected I could use that in the method.
var randomString = "can i use this?";
/*<![CDATA[*/
var username = [[${#authentication.principal.person.isSubscribedTo(randomString)}]];
/*]]>*/
This does not work and I can't test this because my debugger won't get it the method and is not giving back any errors.
How can I use a local javascript variable in a thymeleaf javascript expression?

You're showing a bit of confusion about what's happening when and where.
First, the server uses Thymeleaf to generate the HTML and dynamic Javascript for a page. In this process, as you've said Thymeleaf can call into your Spring beans as it's running on the server.
Then, once the HTML & dynamic Javascript is sent to the browser, it runs the Javascript all client-side.
The only real approaches are:
Generate that randomString on the server side as well, within Thymeleaf or in the model accessible to Thymeleaf.
If you need to generate that string on the client side, have the Javascript make a separate HTTP request ("AJAX") call with that information to the server, and then do something reasonable with the response. That is to say, once the Javascript is sent to the browser, if you need to make more round trips to the server it's on the Javascript to ensure that it happens, as Thymeleaf's role in the page is done.

Related

Using javascript how I can pass input value to mysql query as where condition

I have input and I want to get the input value and use it in mysql query as where condition within javascript script
Like this:
<script>
function get_total_value_row()
{
<?php
$products_sql=mysql_query('select * from product where product_name=a');
$products_row=mysql_fetch_object($products_sql);
?>
</script>
What is the correct value which I should replace it with (a) in where condition to work the javascript script?
JavaScript, as defined in your question, can't directly work with MySql. This is because it isn't running on the same computer.
JavaScript runs on the client side (in the browser), and databases usually exist on the server side. You'll probably need to use an intermediate server-side language (like PHP, Java, .Net, or a server-side JavaScript stack like Node.js) to do the query.
Reference: Send data from javascript to a mysql database

How to pass JSON from Django to Angular

Usually Angular get from HTTP request the JSON from server side (like Django).
But, to accelerate rendering, would like to write down on server side the JSON into a Javascript VAR and let proceed Angular on this javascript variable containing the JSON.
My question are:
1) How to pass this javascript var to angular $scope variable ?
(without HTTP).
2) Is writing down the JSON into the HTML a bad/good practice ?
(given my web app is fairly static).
You can do a simple $scope = {{ some_django_tag|safe }}. I can't see why it would be a bad practice if you're 100% sure that the output will be valid JS. I actually do this a bit to instantiate the scope on the page load.

what is the difference with "test" javascript variable and "#test" variable can i convert var javascript to "#test"

I've seen a servlet between the JSP and the database that is implemented similar this page. the important part for me is this:
param p1fd = new param();
p1fd.setVal(request.getParameter("formDDL"));
where the parameter in jsp code is referenced as "#formDDL"
the problem is that I have formDDL as a javascript variable as such:
var formDDL;
How can I convert the javascript variable to #formDDL?
Is there any other way to I pass this variable to Expression Language written for inserting in mySQL code?
The scriptlet part of the JSP is executed on the server side prior to it being returned to the client. Therefore you can't change its value after the page is sent to the client. You would have to have a preliminary page that sets the value of formDDL in javascript and then call into the server to request the page with the formDDL included as a request parameter or POST data. Basically the same approach that was done in the page you linked.
If you didn't want to have to fully reload the page to change the value of formDDL then you would have to take a completely different approach and look into using Ajax.
Also be mindful of SQL injection attacks when following a similar approach to the one you linked to.

Sending data to external js file without it being visible to user (CodeIgniter)

I want to send a variable from my controller to a view which then uses that variable as a parameter for a Javascript function found in an external file. I know that, for example, I could pass the variable from the controller to the view using an array, and then use:
var quiz_key = <?php echo $quiz_key; ?>;
to pass the php variable to a javascript variable, which I could then use as a parameter for the function. However, in doing this, the variable becomes visible if a user was to View Source and see the html. Is there a way of accomplish this to where the variable would not be visible in this context?
Thanks in advance! I'm still getting the hang of this so any help is appreciated!
No. As javascript is not a server side language, but it runs on the client's browser, it cannot be completely hidden from him. You can hide this piece of code, obfuscate it, minimize it, but it can be found eventually.
You can set up a node.js server on the remote machine, connect to it, send it some variables, then process them and return to the client the result, but that's much more complex sollution.
I'll Vote for using Ajax. nodejs is good but if you are in a hurry you should use Ajax.

pyfacebook and javascript query

I'm using pyfacebook on the backend and javascript on the client side. Now if I want to pass a variable to the javascript from pyfacebook. how would I go about doing that, any ideas?
You can't pass a variable directly, as JavaScript is running on the client (browser), and Python is running on the server.
You could make a XHR (AJAX) request from JavaScript to the server which would then return your values back to JS (JSON could be used here).
Or you could put a hidden field to your markup that would have the value in it's "value" attribute. You could then read that with JavaScript.
ps: your question really isn't related to pyfacebook but Python (or any other server side technology) in general and that has been covered here many many times.

Categories

Resources