Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

javascript - Including <script> tag in <script type="text/template"> tag

I am a new programmer and I have a little question. This is a part of my code:

<nav>
   <ul>
     <li><input type="button" value="HomePage" id="home1" data-page="home"></li>
     <li><input type="button" value="About" id="about1" data-page="about"></li>
   </ul>
</nav>

    <div id="content">
        <div class="page active">
            <div class=firstLoad>
            (some content...)
            </div>
        </div>
    </div>

<script type="text/template" id="home">
<div id="backImage">
  <div id="spinner">
</div>
<div>
   <input type="button" value="..." id="car" class="button">
</div>
<div>
   <input type="button" value="..." id="Car1" onclick="location.href='#'" class="button">
</div>
<div>
   <input type="button" value="..." id="reset" class="button">
</div>
</div>
<script src="location.js"></script>
</script>

My question is how can I use this "location.js" script tag in the html page (id="home") when I call this html page with innerHTML and I change the current appear page? No matter what I have done this script doesn't work for me at all in the html page.

Thank you...:)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

HTML provides no means to include the sequence </script> inside a script element.

XHTML allows you to use CDATA markers or entities, but that isn't an option for HTML. (Note that XHTML served as text/html does not provide this option and browsers will use an HTML parser).

The language used inside the script needs to provide a mechanism to avoid the problem.

In JavaScript, for instance, / and / mean the same thing inside a string, so you can "</script>".

text/template isn't a standard MIME type. Presumably it is a custom template format used by some JS on the page. That JS would need to support some form of escaping mechanism when it is parsing the template. You may have to extend whatever library you are using to process your template.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...