I'm trying to pass reCaptcha sitekey from my Spring MVC to thymeleaf template. I have an idea to reach this using jQuery $("#reCaptcha").attr("data-sitekey", reCaptchaSiteKey);, but can't find out how to get this value in jQuery.
Controller code model.addAttribute("reCaptchaSiteKey", RECAPTCHA_SITE_KEY); Would appreciate your help to solve this problem in this way or any other
There are quite a few ways to go about this. If you just want to use javascript, you can do javascript inlining:
http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#javascript-inlining
<script th:inline="javascript">
var key = [[${reCaptchaSiteKey}]];
$("#reCaptcha").attr("data-sitekey", key);
</script>
You could also use thymeleaf to directly output the #reCaptcha div
<div class="g-recaptcha" th:attr="data-sitekey=${reCaptchaSiteKey}"></div>
Related
I'm trying to use thymeleaf inside javascript.
my code work fine when i use th:each with html
<table>
<tr th:each="theme : ${les_themes_1}">
<td th:text="${theme.id}">id</td>
</tr>
</table>
but when i use forEach inside a script,it return me null
<script th:inline="javascript" >
/*<![CDATA[*/
/*[# th:each="theme : ${les_themes_1}"]*/
document.write(/*[[${theme}]]*/); //return null
document.write(/*[[${theme.id}]]*/); //not working
/*[/]*/
/*]]>*/
</script>
help plz ! thank you!
This kind of question has a pretty long history, going all the way back to the early days of the JSP spec. One key thing to remember about JavaScript is that it executes within your browser based on the output generated from your server-side Java components. This is true for JSP and Javascript, Velocity and Javascript, JSTL and other custom tags and Javascript, as well as Thymeleaf and Javascript. In this scenario, the Thymeleaf template is executed well before the Javascript.
Knowing this, one critical troubleshooting strategy is to simply view and inspect the HTML & Javascript source code you generate at runtime.
i have an ng-repeat but i need to use Angular expression inside javascript of my html page(of course app and controller are declared, but have not been posted here):
<div ng-repeat="eleRubrique in scopeComplet">
<script type="text/javascript">
alert('index ' + {{$index}});
</script>
</div>
That does not work. what would be a workaround ?
Thank you for an answer.
I think this would work for you
<div ng-repeat="eleRubrique in scopeComplet" ng-init="alert('index ' + $index)"></div>
And if you would need a more complex script I would recommend to write a function for that and call the function from ng-init
You should not use double curly braces {{}} in JavaScript. They are meant for HTML only in Angular.
Since the JavaScript code in your HTML has nothing to do with the view, can you not achieve the same goal in the controller?
I am trying to bind data to angular template. But I'm facing the following issue.
I am using $templateCache to retrieve the template but I cannot find data into the template.
$templateCache.get('q1.index.html');
Now, I am also trying to do $compile($templateCache.get('q1.index.html')); but I am getting Error: [jqLite:nosel] http://errors.angularjs.org/1.3.14/jqLite/nosel error.
Can someone please let me know what I am doing wrong & how can I resolve it.
You need to use this format for your <script> tag
<script type="text/ng-template" id="templateId.html">
<p>This is the content of the template</p>
</script>
then somewhere else you can retrieve with $templateCache
$templateCache.get('templateId.html')
Or via ng-include
<div ng-include="'templateId.html'"></div>
I am completely new to jQuery. I can't find any good documentation on the get function and was wondering if I could get some help.
I have an HTML page called me.html with just a single div called me. I want to use the following page to get the contents within the div. Even a google in the right direction would help. Thanks so much
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="data.js"></script>
</head>
<body>
<form id="form" runat="server">
<div>
<div id="me">
</div>
</div>
</form>
</body>
</html>
you don't need to use get(). This simple script should do it
<script>
var contentsOfMe = $('#me').html();
</script>
get is used for loading data from an url. You seem to be wanting to get the contents of a div
as in $("#me").text()
What about using the great documentation provided at http://api.jquery.com/jQuery.get/
EDIT. If you want to just get the text, use var myText=$('#me').html();, and if the html, use var myHtml=$('#me').html();
You will find the documentation for the function get here : http://api.jquery.com/jQuery.get/
But get is to perform an ajax request get on your server so I don't think that's what you need.
In jquery, most of the time you will "select" an element using jquery selector : $("#id")
This will select $(), this will say you are selecting an element using his id $("#name_of_the_id").
Then, you will have an object which will represent the selected element.
If you want to get all the html inside this element do :
function getHtmlFromElementId(id)
{
var element = $("#" + id);
var html = element.html();
return html;
}
Printing the return of this function will print all html code inside the element selected.
If you are seaching for a good tutorial on jquery, the w3schools' one is really good:
http://www.w3schools.com/jquery/jquery_examples.asp
I want to display user typed html (from WYSIWYG).
But I have some problems with removing js from html.
Here is my view:
def bad_view(request):
# in real project we got it from user
bad_html = '<p onclick="alert(0)">:((</p><script>alert(0);</script>'
return render(request, 'template.html', {'bad_html': bad_html})
Here code in my template:
{{ bad_html|safe }}
bad_html should be like this <p onclick="">:((</p>
What is best way to remove ALL js from this html?
For removing <script> I can use regex, but what about onclick handler(and other js handlers)?
Thanks for your advices!
I recommend using the bleach python library, it is designed to handle all sorts of special cases and be a complete solution for your problem
http://bleach.readthedocs.org/en/latest/index.html
Might I suggest a prebuilt solution for django https://www.djangopackages.com/grids/g/forms/
Most have some form of filter examples included. I am actually looking into TinyMCE myself.
More info is here Using safe filter in Django for rich text fields