I'm trying, without success, to add a javascript to a mustache template
<script id="nodeTemplate" type="text/template">
{{#myItem}}
<div class="divclass {{#myItemDet}}">
...
</div>
<script type="text/javascript">
alert('{{#myItemDet}}');
</script>
{{/myItem}}
</script>
Maybe, it is not possible with Mustache to create JS script...
--------------------------- NOTE 1
If a embeed
<script type="text/javascript"> ... <script>
inside a
<script id="nodeTemplate" type="text/template">...</script>
I suspect that mustache close the template at the first tag
Is it true?
Riccardo
--------------------------- NOTE 2
This is my solution:
1) fill with mustache several hidden divs with the class jshidden
2) Search objs with class=jshidden and append div-content as javascript
The solution now work only on Chrome... Still working
http://jsfiddle.net/ric79/zWEUK/
You are using the block form for myItemDet, rather than the value form.
Try this
<script id="nodeTemplate" type="text/template">
{{#myItem}}
<div class="divclass {{myItemDet}}">
...
</div>
<script type="text/javascript">
alert('{{myItemDet}}');
</script>
{{/myItem}}
</script>
Maybe try to obfuscate the ending </script> tag to not have the browser's parser take it for an inline script. Split it apart, just like advertisers do when writing to the document (e.g. document.write('<'+'/script>');).
Related
I have code similar to this
<script type="text/ng-template" id="maintemplate">
DO SOMETHING HERE/SOME MORE DIV TAGS
<script type="text/ng-template" id="zipCodeModal">
<div custominclude = " 'zipCodelookup.html'"> </div>
</script>
<div id="zipCodeModal"></div>
</script>
<div ng-include="'maintemplate'"></div>
This doesnt work as i understand that script tag will look for end of first script tag ('</script>') . I cannot use external file as it fails to load some of the data.
Is there any alternate solution for this
Quoting Handlebars FAQ:
How can I include script tags in my template?
If loading the template via an inlined tag then you may need to break up the script tag with an empty comment to avoid browser parser errors:
<script type="text/x-handlebars">
foo
<scr{{!}}ipt src="bar"></scr{{!}}ipt>
</script>
Nevertheless, I can't seem to be able to implement this:
document.getElementById('output').innerHTML =
Handlebars.compile(
document.getElementById("template").innerHTML
)({});
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.10/handlebars.js"></script>
<script id="template" type="text/x-handlebars">
foo
<scr{{!}}ipt>
alert("Yay!");
</scr{{!}}ipt>
</script>
<div id="output"></div>
As you can see no Yay! is alerted.
How to make this work?
I tried the same with jQuery's .html() and it worked fine. I could narrow down on .innerHTML being the culprit when I looked into this answer.
In short, script tags inserted with .innerHTML needs to be explicitly evaluated using eval().
So below code works, if you eval the script:
document.addEventListener('DOMContentLoaded', function() {
document.getElementById("output").innerHTML = (Handlebars.compile(
document.getElementById("template").innerHTML
))({});
eval(document.getElementById("myscript").innerHTML); // explicitly eval the script
}, false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.10/handlebars.js"></script>
<script id="template" type="text/x-handlebars">
foo
<scr{{!}}ipt id="myscript" src="bar">console.log("Hello")</scr{{!}}ipt>
</script>
<div id="output"></div>
Hello I have a JavaScript code and I want to use it in ASP file but my error code says:
Active Server Pages error 'ASP 0138'
Nested Script Block
/reklamsag.html, line 3
A script block cannot be placed inside another script block.
My code is:
<script src="http://ad.reklamport.com/scripts/rp.js" type="text/javascript"></script>
<script type="text/javascript">
document.write("<script src='http://ad.reklamport.com/rpgetad.ashx?tt=t_canvecan_anasayfa_300x250&ciid=&rnd="+Math.random()%99999999+"'></"+"script>");
</script>
Someone says use code in external file and include it asp file i use this include code but it didn't work:
<!--#include file="reklamsag.html"-->
There is a technique to split the word "<script" into two parts such as "<scr" and "ipt" .
document.write("<scr"+"ipt src....></scr"+"ipt>");
Your code can go like this:
<script src="http://ad.reklamport.com/scripts/rp.js" type="text/javascript"></script>
<script type="text/javascript">
document.write("<scr"+"ipt src='http://ad.reklamport.com/rpgetad.ashx?tt=t_canvecan_anasayfa_300x250&ciid=&rnd="+Math.random()%99999999+"'></"+"scr"+"ipt>");
</script>
That's an incorrect way to load external JavaScript.
I understand the reasoning behind this is to prevent caching, since you already have server side language at your disposal, just have this and it will have the desired effect:
<script type="text/javascript" src="http://ad.reklamport.com/rpgetad.ashx?tt=t_canvecan_anasayfa_300x250&ciid=&rnd=<%=CLng(Timer())%>"></script>
This will append the amount of seconds since 12 AM, which is pretty much the same as random number. If you want extra layer or "uniqueness" you can add year, month and day.
I'm using a javascript template like this:
<script id="template" type='text/tmpl'>
<img src="{{=imagesrc}}" />
</script>
The problem is that loading that template the page would try to make a request to http://site.com/{{=imagesrc}}.
My understanding was that using the script element type should prevent what's inside from being interpreted as html?
How do I fix this? Thanks.
Not sure what you're really trying to do.. but you could try wrapping whatever is inside your script tag with CDATA:
<script>
/*<![CDATA[*/
Your stuff here
/*]]>*/
</script>
I'm trying to embed some code between <script> </script> tags, pyramid however doesn't like it and gives me
ExpatError: not well-formed (invalid token)
Probably because i have && in my code. I tried using & instead, but then it didn't get interpreted in the browser.
The same thing happens when i try to put it in CDATA block.
When I move the code to a separate js file it works. I'd like to keep it in the same file for now, just to enable quick corrections.
So, how should I do it?
EDIT:
I get the same error even for templates as simple as this one:
<html
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal">
<head>
</head>
<body>
<span onclick="alert(true && false);">test</span>
</body>
</html>
I think you're supposed to put && (i.e. two times the HTML entity code).
This should work:
<script type="text/javascript">
//<![CDATA[
// my javascript
//]]>
</script>
Have you tried adding a type attribute to your script tag?:
<script type="text/javascript">
...
</script>
It looks like xhtml issue, as w3c validator reported the same error.
I was thinking if there's a switch to change the document type parsed by chameleon to html, but then it wouldn't be possible to include tal and metal namespaces.
Hence it is expected behavior