2

I'm including a <script> tag in my HTML page which contains a fragment of HTML. I want to get this fragment and insert it into the page. The location of the fragment is on the same server as the page itself, so there are no cross-site issues.

<script id="frag" src="/frag.html" type="text/x-jquery-tmpl"></script>

(btw, I've specified the type of the script as text/x-jquery-tmpl as I've been trying to use jQuery templates to access the script content, even though it's just an HTML literal). I believe the value of 'type' is not a big deal.

Some things I've tried:

// Try inserting frag.html into div hdr
// this attempt, using jQuery templates, doesn't work
$('#frag').tmpl().appendTo('#hdr');

// This is an empty string
var contents = $('#frag').text();

Sorry for all the jQuery, I have even less idea of how to attempt this without it. I'm a javascript ignoramus and need the fluffy protection of jQuery at all times.

6
  • Just for curiosity, why are you inserting html in a script tag, instead of, for example, a display none div? Commented Apr 8, 2011 at 17:31
  • I'm just wondering why you would do this? Would it not be easier to just put it in a html file and retrieve that file or an element on the page? Curious about why you'd do this, that's all... Commented Apr 8, 2011 at 17:33
  • @Andre - it's in a different file, so not embedded in the page. Commented Apr 8, 2011 at 17:37
  • @mcos - Good question! I am trying to factor out markup that's common to a lot of html pages, in a system where any dynamic aspects of html pages need to be generated on the client (no server-side templating). If I had server side scripting I could generate the html parts straight into the pages. That's probably clear as mud :) Commented Apr 8, 2011 at 17:39
  • Hum, ok... So you need it to be loaded along with the page? That's why you don't use, for example $.get('another_page.html')? Commented Apr 8, 2011 at 17:41

2 Answers 2

3

You have to use ".html()":

var contents = $('#frag').html();

Personally I think that's silly, and that ".text()" should work, but I logged a bug on the issue and the jQuery people declined to fix it.

Also, the value of the "type" attribute is in fact not a big deal, but you definitely need to set it to something, and not (of course) the actual JavaScript type.

Sign up to request clarification or add additional context in comments.

4 Comments

Wouldn't the native innerText or textContent properties work?
@Tomalak "innerText" doesn't work in IE for some reason on <script> tags.
.html() only seems to work for me when the script tag is inline, rather than pointing to content using the src attribute.
Ah ... OK, well that's a whole 'nother thing then. In that case I'd just fetch the code with "$.get()" or something.
2

or more fully:

$(document).ready(function(){

  $('#hdr').append($('#frag').html());

});

or without the script tag at all...

    <div id='hdr'>
      //content
      <div id='hdr_add'></div>
    </div>
...

     $(document).ready(function(){
        $('#hdr_add').load('frag.html');
    });

.load() is a shortcut AJAX call that sweet and simply loads content from a give URL into the given element.

2 Comments

Could be .load is the simple solution. I was hoping that putting it in a tag would ensure it got downloaded 'earlier' somehow, to maybe cut down on the appearance of the page getting 'built'. But that's pure speculation on my part.
Hmm, OK. I tried it, and there's no visual impact that I can see of inserting the fragment using load. That'll teach me to speculate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.