0

I have a <script type="text/html"> like below:

<script type="text/html" id="id">
        // I want to import HTML from a file here
</script>

Please suggest any other solution than <object>, <iframe> and <embed> Those tags are messing up my CSS as they insert their own HTML, BODY and other elements.

I would prefer NOT using jQuery. Although not so adamant about it.

EDIT:

script type="text/html" is MUST as I am using templates to route between the pages. And the template is identified by the "id" of this script type="text/html" element.

6
  • Is the linked page on the same domain as the source, or is this cross-domain? Commented Jul 15, 2015 at 20:04
  • You can have your html as jspf and directly include the file using <%@ include file="fileName.jspf"%>. If its external file/content then you need to use iframe. Commented Jul 15, 2015 at 20:05
  • @dave It is on the same domain. Just different folder. Commented Jul 15, 2015 at 20:07
  • 2
    "I would prefer NOT using jQuery." Any particular reason? It certainly is possible without it, but the wrappers jQuery provides for AJAX operations are super convenient. Commented Jul 15, 2015 at 20:32
  • @esqew how can it be done in jQuery? I am not so adamant about not using it. Commented Jul 15, 2015 at 20:37

2 Answers 2

3

If you do consider utilizing jQuery, .load() will do the trick:

$( "#id" ).load( "path/to/file.html" );

JSFiddle illustration

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

5 Comments

You are changing the question itself by that answer. My question is not just to load a html page. I want to load it in my script type="text/html" element.
$( "script[type=text/html]" ).each(function() {$(this).load($(this).text())}); should load it for every script of type text/html.
@liam_ Cannot do this. As I have multiple script type="text/html" elements. Each with different template.
The whole point is that it works with multiple script type="text/html" elements? What's wrong with my code?
@liam_ I have multiple script type="text/html" with different ID's. Based on those ID's I have to plug in the template (HTML pages). How do I do that?
0

If your file is on the same domain, I suggest you use ajax to recover it. Like this:

<div id="wrapper"></div>

<script type="text/javascript">
    var remote_html_container = jQuery("#wrapper");
    jQuery.ajax({
      url: "my-page-url",
      success: function(data){
        remote_html_container.html(data);
      }
    });
</script>

What it does is load the page's output through jQuery and "paste it" into the div#wrapper.

EDIT: It's indeed way easier with the load function:

<div id="wrapper"></div>

<script type="text/javascript">
  jQuery("#wrapper").load("my-url.html");
</script>

3 Comments

How do I include it in the script type="text/html" tag?
Your script/html has no reason to be there anymore. Just put a div with a chosen ID and replace this id inside the javascript part.
Ok then just change #wrapper by #id.

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.