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

by 8 replies
11
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.
#programming #call #code #external #file #html #javascript
  • 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?
    • [ 1 ] Thanks
    • [1] reply
    • 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
      • [ 1 ] Thanks
  • 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.
    • [ 1 ] Thanks
    • [1] reply
    • 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!
      • [1] reply
  • 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(); ?>
    • [1] reply
    • 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.
  • You mean everyone doesn't do everything with WP??

Next Topics on Trending Feed

  • 11

    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)