Home > Articles > Webmaster resources > Syndication tutorial

Syndicating your website content

Content is visitor bait. If you post something sufficiently entertaining, useful or informative on your website, you'll attract visitors. But there is an alternative to just uploading your work and waiting for people to sniff it out. You can send your content out hunting by putting it on other websites. Capture the reader's interest and they'll click through to your site. Even if they don't visit you, you build your reputation each time your work and your name are seen.

This page provides some scripts you can adapt to start syndicating your content to other sites. Put your cursor in the form field with the script, select all, copy and then paste it into your webpage.

Syndicating images

If you're syndicating image files, the easiest thing is to just get people to link to a picture on your site directly. Here's some example code that will take the image cartoon.gif from your site (www.example.com) and embed it in the host page.

<a href="http://www.example.com">
<img src="http://www.example.com/cartoon.gif">
</a>

When you want to change the cartoon, just upload a new version of cartoon.gif. This link works on any browser that has images switched on, but it does mean that you'll have to upload a new file each day if you want to publish a daily comic strip.

Using Javascript to automate the selection

Javascript offers a good alternative, both because Javascript files on other sites can be linked as easily as images can, and because they offer some intelligence to save you time. The code below will show a cartoon numbered for the day of the week so that on the 12th, it will show cartoon12.gif and so on.

<script language="Javascript">
document.write('<IMG SRC="cartoon0.gif" name="tip" BORDER=0>');
today=new Date();
dateno=today.getDate();
if (document.images)
{document.tip.src="cartoon"+dateno+".gif";}
</script>

Using that script means you only need to update your cartoons once a month, and you can do it any time in the month too.

As that script shows, Document.write is used to get Javascript to insert some HTML in the page. The script above works by inserting the default image (cartoon0.gif) and giving it a name, working out the day number in the month, and then replacing cartoon0.gif with the file that has the name "cartoon" plus the date number plus ".gif". If you wanted to let other webmasters use this to host your pictures, you'd need to add your website address and the full path to the images before each mention of "cartoon".

Self-updating syndication code

Using the previous script, you need to get all the webmasters to update their site if you have to change something about the code. There's a better way. You can put just the Javascript (trim off the <script> tags) in a file with the extension .js (we'll call it syndicate.js) and get webmasters to link to it like this:

<script language="Javascript" src="http://www.example.com/syndicate.js">

Here's how it works:

  • You create a Javascript file that writes your HTML to the page using document.write commands
  • You give this file the extension .js (it can have any name, in my example, it's syndicate.js)
  • You then invite webmasters to link to this Javascript using the code above
  • Each time their page is viewed, it will call up syndicate.js from your server and run it
  • Because that Javascript does the job of writing your HTML into the page, it means that effectively you're pasting your HTML into someone else's page in a roundabout way.

The beauty of it is that you can change the syndicate.js code at any time and the next time someone views any webpage calling it, that webpage too will change. So if you link lots of webpages (perhaps many of which you don't own) to the same syndicate.js file, you have a way to update all of these separate pages all over the internet by changing just one file that you control (syndicate.js).

It's much more flexible than our previous daily cartoon example. If you need to alter the path or size of your images, just update your syndicate.js file. You host sites won't even notice the difference, let alone have to update their code.

You can use the same technique to send any HTML code to another website. Just put your HTML inside document.write() code. You need to take care with quotes, because Javascript can think that you're telling it you've finished your document.write when you're using an apostrophe. Javascript uses a leading \ before apostrophes or HTML quotes to avoid confusion, like this:

<strong>Don't let apostrophes confuse</strong>

would become:

document.write('<strong>Don\'t let apostrophes confuse</strong>');

Don't panic though - there's a free Javascript include file creator on this site.

Random tip Javascript

If you're sending short articles to other sites, you can use a similar a trick to our daily image trick above. You can send them all in the same Javascript file (so they must be short!) and then get Javascript to pick one to display.

You store your advice in arrays (we're calling them 'quote' here) and get Javascript to show a random one. Here's the bare bones of the code:

quote=new Array();
quote[0]="Don't let pets play with matches";
quote[1]="Use an umbrella in the rain";
chosen=Math.floor(Math.random()*quote.length);
document.write(quote[chosen]);

Just add new quotes in turn, increasing the number in square brackets.

Daily quote Javascript

This mutant hybrid of our two scripts will show a daily quote:

quote=new Array();
quote[0]="Don't let pets play with matches";
quote[1]="Use an umbrella in the rain";
... ...
quote[31]="Eat yoghurt";
today=new Date();
chosen=today.getDate();
document.write(quote[chosen]);

Dress it up by getting Javascript to insert HTML formatting, write some more useful advice and put it in a .js file and you're ready to syndicate your ideas all over the web.

Related links

Credits

© Sean McManus. All rights reserved.

Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!

Discover my latest books

100 Top Tips: Microsoft Excel

100 Top Tips: Microsoft Excel

Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.

Scratch Programming in Easy Steps

Scratch Programming IES

This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.

Mission Python book

Mission Python

Code a space adventure game in this Python programming book published by No Starch Press.

Cool Scratch Projects in Easy Steps book

Cool Scratch Projects in Easy Steps

Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!

Raspberry Pi For Dummies

Raspberry Pi For Dummies

Set up your Raspberry Pi, then learn how to use the Linux command line, Scratch, Python, Sonic Pi, Minecraft and electronics projects with it.

Earworm

Earworm

In this entertaining techno-thriller, Sean McManus takes a slice through the music industry: from the boardroom to the stage; from the studio to the record fair.

Scratch Homeschooling resources

Scratch cat with balloons invites you to visit my Scratch resources

Sean's Scratch Resources

Tips, tutorials and free book chapters for Scratch, a coding language widely used in schools.

Walking astronaut from Mission Python book Top | Search | Help | Privacy | Access Keys | Contact me
Home | Blog | Copywriting Services | Books | Free book chapters | Articles | Music | Photos | Games | Shop | About