Forum Moderators: coopster

Message Too Old, No Replies

require_once(<html fragment file>) vs document.write(<html fragment>)

which method is better?

         

KBS Gladiator

6:01 am on Apr 14, 2005 (gmt 0)

10+ Year Member



To reuse common fragments of html on many pages I have used javascript functions, called from a linked .js file, which are precisely one instruction:
document.write(<the html>)
.
I'm starting to learn PHP now and see that a
require_once(<url for file containing the html fragment>);
will have the same affect.
My question is, which method is more efficient? i.e. renders faster?
P.S. is there a simple way to set up testing-only server-software on my local machine so i can debug php locally before uploading them to site's server?

[edited by: jatar_k at 4:29 pm (utc) on April 14, 2005]
[edit reason] no urls thanks [/edit]

IamStang

11:29 am on Apr 14, 2005 (gmt 0)

10+ Year Member



I don't see that either way would be faster than the other. So, I would suggest using a method of php as a large number of web users have javascript disabled.

Just my 2 cents worth.

StupidScript

4:46 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Definitely the PHP code will be more stable. Not only are you dealing with people who have Javascript disabled, but the WAY in which the browser handles the incoming code might also be variable, depending on the browser, version, OS, etc.etc.

Server-side operations (like PHP) are always more reliable. If you are concerned about delaying the page delivery or increasing the server's processor load by using the PHP solution, there will be virtually no difference (ok, a tiny bit more processor load with PHP ... but really negligible) between using PHP or Javascript for this.

PLUS, using PHP to include the code offers the opportunity for you to re-use code quite simply. Where with Javascript you would need to alter the document.write() text on each page that includes it, with PHP you would only need to change the one required file, and then every page that uses it will automatically be updated.

Oh ... and welcome to WebmasterWorld! :)

StupidScript

4:52 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is there a simple way to set up testing-only server-software on my local machine so i can debug php locally before uploading them to site's server?

What kind of site server are you running? You need to duplicate that setup on your testing machine. If you use MS IIS, then install IIS on your testing machine. If you use Apache, then it needs to be installed.

Once your server is installed, download and install PHP for that server, start the server, and browse to [127.0.0.1...] to view your local website.

With the Apache option, there are several "ez-install" packages available that will install both Apache and PHP (and MySQL database server, if you like). With IIS, you'll need to download the Windows PHP file(s) and install them separately.

KBS Gladiator

10:14 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



StupidScript said:

PLUS, using PHP to include the code offers the opportunity for you to re-use code quite simply. Where with Javascript you would need to alter the document.write() text on each page that includes it, with PHP you would only need to change the one required file, and then every page that uses it will automatically be updated.

Well actually, I find the .js solution simpler - I probably wasn't clear: say I have a fragment of html for a banner to appear on each page. I put a

<script type="text/javascript">embedbanner();</script>
tag on each site page then in my .js file i have
function embedbanner(){document.write('<all the html for the banner>')}
. So if I change the banner, I only need to change it once in the .js file. I say i find it easier than a
require_once("<url to file containing banner html fragment>")
because I can have several such embed functions in the same .js file(like a footer, nav, etc) whereas I (think) I need one file for each frag if I used the PHP solution. Also - having one central js file with all these embedXXX() functions linked in the <head> means its cached no?
But still, JS turned off IS a serious concern - do people actually surf the web with it turned off? Ca'nt imagine any of our young hip visitors (gamers) that have JS disabled.

<embarrased>Thanks for the server-software info - I have a lot to learn though to understand your advice. e.g. How to find server info?(it's apache, I think), where to get ez-install pks, etc. </embarrased>

StupidScript

10:50 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see. That's not too bad.

Here's an example of a PHP solution, like that:

INIT.PHP

<?php

$topbanner="banner.php?id=top";

$midbanner="banner.php?id=mid";

$lowbanner="banner.php?id=low";

?>

PAGE1.HTML

<?php include "init.php"?>

Top Stuff ...

<?php include $topbanner?>

Body Stuff ...

<?php include $midbanner?>

Footer Stuff ...

<?php include $lowbanner?>

BANNER.PHP

<?php

switch ($_POST["id"]) {

case "top":

echo "Top banner text";

break;

case "mid":

echo "Mid banner text";

break;

case "low":

echo "Footer banner text";

break;

}

?>

You end up with (1) init file that sets up the top, mid and footer banner text references (this is new and more than you currently use, but you never need to touch this file again) and (1) banner file that behaves similarly to your included .JS file (provides different text depending on which banner position).

Then, similarly to what you are doing with Javascript, you indicate in your "real" page where the banner is to go, and which one to use. init and banner take care of the rest.

You make your changes to banner.php for the whole site.

Re: ez-install packages
While there are several out there which work very well, might I suggest XAMPP [apachefriends.org]? It's free, quite robust and very easy to install...

[edited by: StupidScript at 11:03 pm (utc) on April 14, 2005]

StupidScript

11:00 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh... and one more nice thing about using PHP instead of Javascript ...

Some search engines count the words in your SCRIPT tags as part of the whole page content, which, of course, is less than useful to your keyword density figures.

Using PHP eliminates this possibility. All those search engines will see is the result of the PHP operation, no "extra" characters to clog up/diminish your search engine placement work. It also makes each page slightly smaller for the download.

(1 character = ~1 byte:

<script type="text/javascript" language="javascript">
</script>

= +63 bytes to the filesize. Multiply x number of SCRIPT tags on the page to get the full filesize reduction you would receive by replacing those SCRIPT tags with a PHP operation, where feasible.)

iceman22

7:12 pm on Apr 15, 2005 (gmt 0)

10+ Year Member



I see you've included php files with query strings, I have been unable to do that in the past. I just copied what you did and got a "No such file or directory" because of the query string. Is there something to do to get it to work or does PHP 5 handle including filename differently or something?