0

Ok, so I was wondering if it was possible to use the code

<script type="text/HTML" src="Other.HTML">

to get one bit of code from one HTML file to another?
EDIT: Ok, just tried it. No luck, all I am getting is a blank screen. Let me rephrase it in is there any way to make this possible.

  • Why don't you try it? –  Feb 03 '14 at 01:14
  • 1
    check this one: http://stackoverflow.com/questions/8988855/include-another-html-file-in-a-html-file – Vamshi Feb 03 '14 at 01:15
  • 1
    This question represents a basic misunderstanding of HTML and shows no effort by the poster, despite containing all code necessary to answer the question. – elixenide Feb 03 '14 at 01:20
  • Oh please. It's a fine question, and an original attempt. (It's not possible, though, @XanaWarriors; there is actually no way to do this without a server-side language.) – Pekka Feb 03 '14 at 01:20
  • Actually Lee Taylor, it is, because now I am asking for advice to do this, seeing as the code I gave was not successful. Before I had asked if anyone thought it would work, and am now asking if anyone can help me get a way to MAKE IT work. – XanaWarriors Feb 03 '14 at 01:22
  • 1
    +1 to @Vamshi for the only constructive response. This community is becoming more depressing every day. – Pekka Feb 03 '14 at 01:22
  • Welcome to the world of dynamic programming.. – HTTP Feb 03 '14 at 01:22

1 Answers1

2

Pure HTML (without JavaScript) cannot load in another page on it's own. However, you can use PHP, SHTML, or JavaScript to obtain another page and load it into the page.

PHP

Make sure that your HTML file has been renamed with the .php extension. Then you can add the following line where you want to add the HTML file:

<?php include('some_file_name.html'); ?>

As long as some_file_name exists, that page will load in where the PHP tag is.

SHTML

Make sure that your HTML file has been renamed with the .shtml extension. Then you can add the following line where you want to add the HTML file:

<!--#include virtual="some_file_name.html" -->

Again, as long as some_file_name exists, the page will load in the page at that. Be sure to watch for spacing, especially between the second dash and #, as the line of code will break if there is a space.

JavaScript

The plus side of using JavaScript is that you can keep your HTML file a .html file. However, more code is needed to load in the included file:

<script src="jquery.js"></script> 
    <script> 
    $(function(){
      $("#includeThis").load("some_file_name.html"); 
    });
    </script> 
...
<div id="includeThis"></div>

This will create a function that will load some_file_name.html where the div id of includeThis is located. This contains more overhead, but you can keep your .html file extension.

Mike Koch
  • 1,540
  • 2
  • 17
  • 22
  • Ok, this did work, however it brought over the whole file. I was wondering if I could move only the variables. – XanaWarriors Feb 03 '14 at 01:31
  • What do you mean by a "variable"? You mean only a portion of the page? – Mike Koch Feb 03 '14 at 01:32
  • 1
    As long as you don't have any scripts in the file, you can select specific elements to load with any generic/specific element selector like this: `$("#includeThis").load("some_file_name.html #elementID")` ` or it could be `.elementClass` that selects multiple elements with the same class to be loaded. – Deryck Feb 03 '14 at 01:33
  • @Deryck Wow, I learned something new based off of my own answer. Never knew you could do that before; I was always using PHP "functions" that held the content I needed! – Mike Koch Feb 03 '14 at 01:34
  • 1
    haha it's very touchy though if you end up with a script inside it'll throw a fit but otherwise it's a handy little tool. – Deryck Feb 03 '14 at 01:36