Valhalla Legends Archive

Programming => Web Development => Topic started by: Lenny on June 17, 2005, 12:42 PM

Title: Inserting one html file into another
Post by: Lenny on June 17, 2005, 12:42 PM
Is there any way to insert one html file into another using javascript or html?

To clarify, is it possible to append to the bottom of one html file text from another file?

Frames would seem like a very expensive way to do this, especially when there could be hundreds of them.
Title: Re: Inserting one html file into another
Post by: R.a.B.B.i.T on June 17, 2005, 06:33 PM
Use php!

<?php    // this is index.php    echo "omg blah blah blah brbbbq<BR>";    include('morerandomcrap.htm');?>


<!--
this is morerandomcrap.htm
//-->
<h1>OMGROFLWAFFLE!!!</h1>


As for JS, I don't know how, but I'm sure there's a way.  As for doing it in HTML, not possible.
Title: Re: Inserting one html file into another
Post by: Blaze on June 17, 2005, 07:32 PM
Do NOT use include.


<?phprequire_once "Blah.html";?>


Thats the best of the 4 requires/includes.
Title: Re: Inserting one html file into another
Post by: Arta on June 17, 2005, 08:56 PM
Why?
Title: Re: Inserting one html file into another
Post by: Blaze on June 17, 2005, 09:16 PM
You could go off in a loop if a page is poorly constructed.

Example of a poorly constructed page:

<?php     $bob = $_GET[page]     include "../".$bob?>



index.php?page=index.php would loop on forever.

Use require so the page will stop loading as soon as it hits the error.
Title: Re: Inserting one html file into another
Post by: Akamas on June 17, 2005, 09:17 PM
I would use,

<?
echo file_get_contents('file.html');
?>


I would use this because PHP will parse the file if you use include() or require(). Considering that you know that there isn't any PHP in "file.html", its just best to use file_get_contents, because all its doing is reading the file and spitting out the data.
Title: Re: Inserting one html file into another
Post by: Warrior on June 17, 2005, 09:19 PM
It also allows you to construct an optional templating engine replacing tags that you define :]
Title: Re: Inserting one html file into another
Post by: Arta on June 17, 2005, 09:29 PM
Quote from: Blaze on June 17, 2005, 09:16 PM
You could go off in a loop if a page is poorly constructed.

In other words, it hides bad design? require_once wouldn't fail on a recursive or circular include.

You'll notice a bad include pretty quickly whther you're using require or include. What does it matter?

Prior to version 4.0.something, require is not affected by conditional statements. In other words, this require would always execute:


if(false)
{
   require 'x.php';
}


Thus, I contend that require is bad; include is much more reliable and no less error prone, assuming you have warnings enabled on your development box.
Title: Re: Inserting one html file into another
Post by: Warrior on June 17, 2005, 09:32 PM
sometimes you have code that can't be declared more than once (classes) and you may have a file that accidently includes it once (and to save you the trouble of worrying about it) include_once makes sure not to re-include if it already has been.
Title: Re: Inserting one html file into another
Post by: Arta on June 17, 2005, 09:43 PM
Accidentally being the key word. Languages shouldn't help programmers desguise bad design choices. require_once and include_once are both things that lull one into a false sense of security - rather like "on error resume next".

Require is fine for most things, but problem-prone on servers running old versions of php. Include is safer, and is thus a better choice.
Title: Re: Inserting one html file into another
Post by: Lenny on June 17, 2005, 10:02 PM
Well as far as using javascript goes...

Is it possible to do anything similar to a countdown, where a number is displayed after 1 second has passed?

Quote
5
4
3
2
1

I'm having issues where window.setTimeout() and window.setInterval() only fire once with document.write()'s...