ReCaptcha & Underscore.js Templates - backbone.js

I am running into an issue in my backbone/underscore application. Our site uses recaptcha and we want to put that content inside a view. We are using underscore for templates. How would i put the recaptcha code inside a template? THe problem is there are scripts tags required for recaptcha and it collides with the underscore script tag. For example it would look something like this:
<script type="text/javascript" id="someTemplate">
<div>
some html here
</div>
<script type="text/javascript" src="https://www.google.com/recaptcha/api/challengek=YOUR_PUBLIC_KEY"
 
</script>
any help is appreciated. Thanks!

Underscore does not prevent you from using script tags, your problems come from your template declaration : you use type="text/javascript" which means your browser tries to interpret your template as Javascript and you get erratic results.
Try
<script type="text/template" id="someTemplate">
<div><%= text %></div>
<script type="text/javascript"
src="https://www.google.com/recaptcha/api/challengek=<%= key %>"
/>
</script>
and a demo http://jsfiddle.net/VxjBs/
As you noted in the comments, Recaptcha tries to loads a second script via document.write and fails when inserted in the DOM (see Can't append <script> element for a probable explanation).
Your best bet is probably to go through Recaptcha Ajax API, generate your HTML, identify a node and apply Recaptcha.create on it. Something like
<script type="text/template" id="someTemplate">
<div><%= text %></div>
<div class='recaptcha'></div>
</script>
The basis for a view could be
var html = _.template(src, {
text: 'in div'
});
var $el = $('#render').append(html);
$el.find('.recaptcha').each(function(idx, el) {
Recaptcha.create(
pubkey,
el
);
});
http://jsfiddle.net/nikoshr/VxjBs/2/

Not sure what all the reCAPTCHA script does, but I'm assume it tries to append HTML right after itself. If that's the case then you will probably need to manually attach a script node to the view after you've rendered it and then set the src of the script node to the URL of the external javascript file.
You cannot put script tags inside an underscore template as the browser will only parse the outer-most script tag (your template).

The proposed solution is too complicated.
This can be achieved very easily as follows (in fact I've just implemented it in my project).
Make sure to include the recaptcha js file in the "head" element of your page as follows:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
Add this function in your javascript somewhere.
var render_recaptcha = function(target_id) {
grecaptcha.render(target_id, {
'sitekey' : RECAPTCHA_SITE_KEY
});
};
Then, just call this function after you render your template:
<script type="text/template" id="someTemplate">
<div><%= text %></div>
<div id='recaptcha'></div>
</script>
//render template like you usually would
//...
//then render the recaptcha
render_recaptcha('recaptcha');
That's it.

Related

Connecting HTML iFrame to database

I have a database connected to dynamic pages. One of the columns in the database is an HTML embed code that I would like embedded on each page.
Is there a way of hardcoding the content of the database to an HTML Embed Frame via Velo? If so, how can this be done exactly?
There is no built-in way (click and play) but you can pass the html using postMessage from the website to the iframe.
For example:
Website code:
$w('#html1').postMessage(htmlFromTheDatabase)
HTML component code
<html>
<body>
<div id="content"></div>
<script>
window.addEventListener('message', message => {
console.log('message', message.data);
document.querySelector('#content').innerHTML = message.data
});
</script>
</body>
</html>
https://moshef9.wixsite.com/html-from-db

how to prevent script tag injection/load before rendering html in angularjs?

var testApp = angular.module('testApp',['ngSanitize']);
testApp.controller('TestController',function($scope,$sce){
$scope.htmlString = "<span>Test HTML</span><script>alert('Hello Script');</script>";
$scope.toTrusted = function(htmlContent) {
if(htmlContent && htmlContent != "") {
return $sce.trustAsHtml(htmlContent);
}
return "";
};
});
<html ng-app="testApp">
<body ng-controller="TestController">
<div ng-bind-html="toTrusted(htmlString)"></div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.11/angular-sanitize.min.js"></script>
<script type="text/javascript" src="resources/js/controllers/testController.js"></script>
</body>
</html>
I am using ng-bind-html for rendering string as html.Sometimes i am getting script tag in html string and i don't want to inject/load script tag.So what should i do to prevent script tag.
I have also searched the solution for this issue,somebody says that just remove script tag from string before rendering html.So please somebody suggest me.What is the better way to do that?
I am developing chat app with the help of angularjs and firebase.And for rendering chat messages i use ng-bind-html directive.What I have noticed that if user send script tag as part of chat messages but given script is being run.So I want to restrict this script to run.
If you are using $sce.trustAsHtml, you need to use ng-bind alone.
<div ng-bind="toTrusted(htmlString)"></div>
Try this and let me know.

Angular Dropdown (could be just IE): strange positioning of dropdown in IE

I'm taking baby steps with Angular and writing simple stuff and testing across browsers, I have a simple script to bind a menu against a JSON string array. I want to do the whole Angular MVC instead of Javascript DOM manipulation.
In my tests I can see a strange behaviour as to the positioning of the top of the menu in IE dependent upon which item is selected. Anyone know how to fix this? I would like to use an Angular friendly solution, like Bootstrap?
Menu looks good in Firefox and Chrome.
<html ng-app="myNoteApp">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<div ng-controller="myCarSelector">
<h2>Menu</h2>
<p>Make of car : <span ng-bind="selectedCarMake"></span></p>
<select ng-model="selectedCarMake" ng-options="val for val in carmakes"></select>
</div>
<script>
// was in separate file but pasted in for demo purposes
var app = angular.module("myNoteApp", []);
</script>
<script>
// was in separate file but pasted in for demo purposes
app.controller("myCarSelector", function ($scope) {
$scope.selectedCarMake = "BMW"; // default value
$scope.carmakes = ["Audi", "BMW", "Volkswagen"];
});
</script>
</body>
</html>
I like to accept answers and then move on to next question.
Here is a screen grab of the problem in IE 11
It seems browser default behaviour.

angularjs specific page without ng-incude

I design my pages with angularjs like following
<html>
<head></head>
<body>
<ng-view></ng-view>
<ng-include="'footer-tpl.html'">
</body>
</html>
so whenever navigate to any pages will just change the ng-view, but now I want to have a
page without the <ng-include="'footer-tpl.html'">. How to do that?
I just realized you can use ngHide with ngInclude:
http://plnkr.co/edit/BBpfQBRgtr7tAK6iFRTR?p=preview
HTML
<div ng-include="'footer.html'" ng-hide="hideFooter"></div>
JavaScript
angular.module('test', []).controller('test', function ($scope) {
// TODO set this to true to hide the footer, if you don't set it, it stays visible
//$scope.hideFooter = true;
});
When this patch for ngIf makes it into a release, you can use it in place of ngHide. It seems like it'll prevent footer.html from even being loaded if you hit the right view, but I'm not totally sure on that.

how to put all templates within different HTML file like "AppTmpl.html" and how to access those templates in backbone?

my application templates are working if they are declared within index.html file.
But, I want to keep all templates in different html file,
like,
-AppTmpl.html
<!-- SidebarView Template -->
<script id="sideBarTmpl" type="text/template">
<div class="inner_div"><img src="images/com_logo.png"></div>
<div class="inner_div"><img src="images/adv_index.png"></div>
</script>
<!-- HeaderView Template -->
<script id="folderDivTmpl" type="text/template">
<div id="div_<%= menu_sequence %>"></div>
</script>
I am using underscore templates.
But problem is that these templates are not accessible by underscore.
like,
this.template = _.template($("#headerTmpl").html(), this.model.toJSON());
So what i need to do like include "AppTmpl.html" file in index.html or something.
Any solutions?
Thanks in advance !
I use requireJS with the text plugin
Then you can just load it as you need with requireJS:
require(['text!templates/AppTmpl.html'],function(AppTemplate){
this.template = _.template(AppTemplate);
});

Resources