Copy and Paste JavaScript With an Ajax Engine / Page 3 | WebReference

Copy and Paste JavaScript With an Ajax Engine / Page 3


Copy and Paste JavaScript with an Ajax Engine [con't]

An Ajax "Same Domain" Restriction Workaround

As noted earler, the main JavaScript function for the Ajax engine may retrieve content only from the domain where the Web page with the JavaScript is located. A workaround is to install a software relay on the domain. Ajax provides the URL to the relay. The relay grabs the content from the Internet and returns it to Ajax. Here's a PHP script and a Perl CGI script. Either one can be used as a software relay.

PHP relay script:

Perl CGI relay script:

Install the script of your choice (only if you wish to retrieve content from a domain) other than where the JavaScript Ajax engine is being used. Notice that both scripts require a list of domain names where they're allowed to retrieve content. This is to reduce issues with hackers/crackers exploiting your relays and grabbing content for potentially malicious purposes.

Leave the instructions in the scripts so they'll be there when you wish to add or remove a domain name.

To use the software relay, make a link something like this:

When the above example link is clicked, the JavaScript retrieves the content from URL /relay.php? and inserts it into the HTML tag with id="AnIdentification".

When URL /relay.php? is asked for content, relay.php grabs the content from URL and returns it to the Ajax engine. /relay.php? is a relative URL. The relative URL's parameter is the absolute URL of the content to retrieve from the domain. The Perl CGI software is used the same way. It's URL might be /cgi-bin/relay.cgi?


You now have a of the copy and paste JavaScript Ajax engine as one of your software tools. And you didn't need to dig into Ajax to understand it. You also have a neat little workaround for the Ajax "same domain" restriction. You can retrieve content at any public HTTP URL and display it on your page.

About the Author

Will Bontrager has created software specifically for the Internet since early 1998. To learn more about Will, visit and

Original: October 31, 2007 Digg This Add to