Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Pull Remote Content Into Web Page (Super Easy)

There are several ways to pull remote content into a web page. This article describes a super easy method using Ajax.

  1. Put one copy-and-paste JavaScript function into your web page, or import it from an external file.

  2. Make a div with an id value.

  3. Call the JavaScript function with (i) the URL to retrieve and (ii) the id value of the div where the content will flow into.

All done.

In this article, "remote content" means content that can be obtained with a URL. Generally, this would be a URL for the same domain as the web page. Content from other domains would need to allow Ajax requests.

Why?

Here are some ways website owners can benefit when publishing content using this system. Let this list stimulate ideas about how you could benefit by using the system at your own sites.

  • News or other frequently updated content duplicatedon several pages can be maintained at one central page.

  • A subscription form to be published on more than one page from one central file. In fact, pretty much any form gets a bonus benefit, even when they aren't repeated on multiple pages — publishing forms with Ajax is a way to hide them from form-spamming robots.

  • Terms of service to be published in more than one place. When updating is required, only one central file needs to be touched — no more comparing several files to ensure they all read the same.

Life Example

Here is a live example using the system. The ad in the scrolling div is pulled in from the https://www.willmaster.com/possibilities/52/GiftYourself.php URL.

[content to come]

Implementation

To implement, the first thing to do is to put this JavaScript function anywhere in the web page source code. It can be pasted into the page or brought in from an external file.

<script type="text/javascript">
function PutURLintoDiv(url,id)
{
   var notAvailable = function() { document.getElementById(id).innerHTML = 'Not available.'; }
   var http = new XMLHttpRequest();
   if(!http) { notAvailable(); return; }
   http.onreadystatechange = function()
   {
     if(http.readyState == 4)
     {
         if(http.status == 200) { document.getElementById(id).innerHTML = http.responseText; }
         else { notAvailable(); }
      }
      else { notAvailable(); }
   }
   http.open("GET",url,true);
   http.send();
}
</script>

No customization is required with the above JavaScript. Just copy it and paste it into place.

To hook up the system, you'll need a div to flow the external content into and a line of JavaScript to get the content.

The div to flow the content into —

The div can be any div with an id value. Here is an example. It's id value is my-id.

<div id="my-id" style="border:1px solid #ccc; padding:.5em;">
[content to come]
</div>

The my-id id value may be changed to any other valid id value. And the div itself may, of course, be styled as you prefer.

The line of JavaScript to get the content —

The line of JavaScript (one line between script tags) to get the content can be anywhere on the page so long as it is somewhere below the div where the content is to be flowed into. Here's the JavaScript (two places to customize).

<script type="text/javascript">
PutURLintoDiv("/subdirectory/page.php","my-id");
</script>

Customization:

  1. Replace /subdirectory/page.php with the URL of the content to be pulled in. A relative URL (as in the example) is preferred when pulling in from the same domain as the location of the web page.

  2. Replace my-id with the id value of the div where the content will flow into.

You're done.

Here's an implementation recapitulation.

  1. Put the copy-and-paste JavaScript function somewhere into your web page.

  2. Create a div to flow the content into.

  3. Create the line of JavaScript to get the content and flow it into the div.

You see how super easy it really is.

(This article first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC