I'm using ajaxify.js ( https://github.com/browserstate/ajaxify/blob/master/ajaxify-html5.js ) to load page content. This script by default loads scripts from the page asynchronously as follows:
$scripts.each(function(){
var $script = $(this),
scriptText = $script.text(),
scriptType = $script.attr('type'),
scriptId = $script.attr('id');
scriptNode = document.createElement('script');
if(scriptType) {
$(scriptNode).attr('type', scriptType);
}
if(scriptId) {
$(scriptNode).attr('id', scriptId);
}
scriptNode.appendChild(document.createTextNode(scriptText));
contentNode.appendChild(scriptNode);
});
The issue is that with my underscore template, using $script.text()
ignores the HTML content of the template completely.
On the other hand, using $script.html()
escapes the <
and >
characters from the template code (underscore's <%
etc...) and the output ends up looking like:
<% _.each(things,function(thing,key,list){ %>
How do I get the script's inner html/text intact so that it functions properly with an AJAX load? Thanks!
The full template script is:
<script type="text/html" id='furniture-template'>
<div class="accordion collapse">
<div class="accordion-group">
<% _.each(things,function(thing,key,list){ %>
<div class="accordion-heading">
<a class="no-ajaxy accordion-toggle ic-minus block collapsed" data-toggle="collapse" href="#things-<%= thing.slug %>">
<%= thing.title %>
</a>
</div> <!-- header -->
<div id="things-<%= thing.slug %>" class="accordion-body collapse">
<div class="accordion-inner">
<% for(var item in thing.items) { %>
<div class="item">
<% if( thing.items[item].images == true ) { %>
<a class="no-ajaxy" data-target="<%= thing.items[item].slug %>-gal" class="img-link ic-cam fl" title="View an example"></a>
<% } %>
<a
class="item-add ic-plus"
data-title="<%= thing.items[item].title %>"
data-slug="<%= thing.items[item].slug %>"
data-img="<%= thing.items[item].images %>"
data-shorthand="<%= thing.items[item].shorthand %>"
data-price="<%= thing.items[item].price %>"
>
<%= thing.items[item].title %>
</a>
</div>
<% } %>
</div> <!-- inner -->
</div> <!-- accordion-body -->
<% }); %>
</div>
</div>
</script>
Curious situation. The simple solution would be to use .html()
and unescape escaped underscore sequences, which is probably what you want (ie you probably don't want to unescape everything):
$script.html().replace(/<%/gi, '<%').replace(/%>/gi, '%>')
您没有尝试过这个:
contentNode.innerHTML = scriptnode;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.