2

I have my adress written in 30 html pages on my website. When I move I am going to want to change the address. Instead of going through the code and changing it for all pages, I am looking for a method to change it in one place.

What would be the best practice for this sort of problem? There will probably not be that many references that would need to be stored. Any help on the matter will be appreciated.

Elias Wick
  • 483
  • 6
  • 21
  • 3
    You're looking for server-side code or a static site generator (like Jekyll). – SLaks Aug 13 '19 at 16:27
  • 1
    If using PHP as your tags suggest, then your PHP files allow you to interpolate values with something like ``. Store the value in a database on the server and load it into the $myAddress variable from there, and whenever you change the database value, it'll change everywhere on your page. – IceMetalPunk Aug 13 '19 at 16:42
  • 2
    @IceMetalPunk this sounds like the perfect solution for me! I wish you would have commented it so that I could have given you the acceptable answer status! – Elias Wick Aug 13 '19 at 17:08

1 Answers1

3

There are literally dozens of ways to do this kind of thing. You could rewrite your website in any of the wide variety of server side languages, whether it's a scripting language like PHP or a compiled language like C# or Java.

You don't have to go that drastic, though. You can simplify it by including references in your pages. This can be a direct usage of another HTML file, or it can be a JavaScript file. The link below shows how to do the HTML file linkage using JQuery, as well as JavaScript. JavaScript doesn't have to be difficult and it can be referenced from all your pages, so it can have all the things in one file that you want to change, allowing you to change one file and it reflect on all your other pages.

HTML include with JQuery: (if you aren't already using JQuery, I'd hesitate to use a whole library to do one function that can be done in vanilla JS just as easily)

a.html:

<html> 
  <head> 
    <script src="jquery.js"></script> 
    <script> 
    $(function(){
      $("#includedContent").load("b.html"); 
    });
    </script> 
  </head> 

  <body> 
     <div id="includedContent"></div>
  </body> 
</html>

b.html:

<p>This is my include file</p>

Or JavaScript:

a.html:

<html> 
  <body>
  <h1>Put your HTML content before insertion of b.js.</h1>
      ...
  <script src="b.js"></script>
      ...
  <p>And whatever content you want afterwards.</p>
  </body>
</html>

b.js:

document.write('\
\
    <h1>Add your HTML code here</h1>\
\
     <p>Notice however, that you have to escape LF's with a '\', just like\
        demonstrated in this code listing.\
    </p>\
\
');

Include another HTML file in a HTML file

If you are using PHP already, there's a good chance you can do it with the PHP you're already using by reading from a text file and inserting the HTML text into the page you are generating.

<?php
// do php stuff

include('fileOne.html');
// or //
readfile('fileTwo.html');

?>

how to embed html files in php code?

computercarguy
  • 2,173
  • 1
  • 13
  • 27