Can you call HTML code from an external file? (with JavaScript)

by Blakos
8 replies
I am trying to work out how I call can HTML code from an external file(assuming with JavaScript) that I can call later on with a function with my scripts.

My goal is to break down my AdSense ad codes into files so I can call them in the rest of my HTML files, making life easier when I wish to change/edit ad code etc.(taking in mind I may very well have hundreds of pages for this project, quickly)

Possible?

Another way around it?

Thanks in advanced.

EDIT: Typos.
#call #code #external #file #html #javascript
  • Profile picture of the author wayfarer
    You probably want to look into doing some sort of server-side include. How you do that depends on how the pages are deployed. Are they static .html pages, or something else?
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[8084766].message }}
    • Profile picture of the author Brandon Tanner
      Easiest way to do that is to use PHP 'includes'...

      PHP Tutorial - Include File

      You can do something similar using Javascript, but it won't be quite as robust as the PHP option, because a tiny percentage of your website visitors will have Javascript disabled. But if you're still interested, this is probably the easiest Javascript way to do it (using jQuery / Ajax)...

      .load() | jQuery API Documentation
      Signature

      {{ DiscussionBoard.errors[8084870].message }}
  • Profile picture of the author FirstSocialApps
    There are a couple of ways to do something like this:

    1) With JQUERY
    Code:
    <html>    <head>      <script src="jquery.js"></script>      <script>      $(function{       $("#includedContent").load("b.html");      });     </script>    </head>     <body>       <div id="includedContent"></div>   </body>  </html>
    2) With a server side include
    Code:
    <!--#include virtual="a.html" -->
    3) With PHP
    Code:
    include('a.php');
    Lastly something many people dont know is that if your files are currently HTML
    and you have so many of them that its not practical to change them all over
    to .php you can always add .htaccess rules to run HTML files through the PHP
    parser.
    {{ DiscussionBoard.errors[8085164].message }}
    • Profile picture of the author Blakos
      Thanks guys,

      I think I will have to work it out with PHP, thanks for those links.

      Although I am stepping into PHP with only PAWN, LUA, HTML, CSS, JS, JQUERY and AJAX knowledge, I will work it out but can you answer a couple of questions please?

      1. Please explain how .htacess works?
      2. Can you also explain how a basic PHP structure looks like? Like index.php + header.php + footer.php etc. etc.

      And anything else you can explain would be great, its my first time in this pond

      THANKS IN ADVANCED GUYS!
      {{ DiscussionBoard.errors[8086145].message }}
      • Profile picture of the author Brandon Tanner
        Originally Posted by Weponz View Post

        1. Please explain how .htacess works?
        That question is way too broad to give a meaningful answer to, as .htaccess can be used for a gazillion different tasks. "How .htaccess works" could fill an entire book (and I'm sure it has). Google is your friend here.

        Originally Posted by Weponz View Post

        2. Can you also explain how a basic PHP structure looks like? Like index.php + header.php + footer.php etc. etc.
        Not sure what you mean by "basic PHP structure", as that is a very subjective question and will depend on your specific goals and preferences for each website. But judging by the file names used in your question, I'll assume that what you really want to know is "what should I use PHP includes for?". If that's what you're getting at, then the answer (in my opinion) is "any code that will be used more than once!"

        For example, say you have a 10 page website, and each of those pages share the exact same header, sidebar, and footer. Now you could copy and paste the same header, sidebar, and footer code into each of those 10 pages. But that would make your code far more bloated than it needs to be, and it would also make it FAR more difficult to maintain.

        A much better option is to make a single header.php file (that contains your header HTML code). Then do the same thing for your sidebar.php file and your footer.php file.

        Then on each page that uses those files, all you have to do is 'include' them wherever you want them to appear on the page. For example...

        HTML Code:
        <!-- Header section -->
        <?php include('header.php'); ?>
        
        <!-- Main content section -->
        <div id="content">(Content that is unique to this page goes here)</div>
        
        <!-- Sidebar section -->
        <?php include('sidebar.php'); ?>
        
        <!-- Footer section -->
        <?php include('footer.php'); ?>
        That way, when you need to make an edit to, say the sidebar, all you have to do is open up the sidebar.php file, make your edit, and save it. Then the change will automatically appear on all of the pages that 'include' the sidebar.php file.

        Make sense?
        Signature

        {{ DiscussionBoard.errors[8087236].message }}
  • Profile picture of the author WPcrew
    Just a little bit correction - all includes should be done in a WP way:
    Code:
    <!-- Header section -->
    <?php include('header.php'); ?>
    
    to
    
    <?php get_header(); ?>
    
    
    <!-- Sidebar section -->
    <?php include('sidebar.php'); ?>
    
    to
    
    <?php get_sidebar(); ?>
    
    and
    
    <!-- Footer section -->
    <?php include('footer.php'); ?>
    
    to
    
    <?php get_footer(); ?>
    {{ DiscussionBoard.errors[8098649].message }}
    • Profile picture of the author Brandon Tanner
      Originally Posted by WPcrew View Post

      Just a little bit correction - all includes should be done in a WP way:
      Ummm... not if you don't use WordPress, they shouldn't! The OP made no mention of using WP, so I assumed that they are not.
      Signature

      {{ DiscussionBoard.errors[8099188].message }}
  • Profile picture of the author wayfarer
    You mean everyone doesn't do everything with WP??
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[8099433].message }}

Trending Topics