FREE tutorial,solution,RSS Feeds on Operating Systems, Programming, Web Development, Applications, Databases, Networking, Hardware, Security, SEO Free Expertsforge Membership
Join us as Moderator
Submit Article to Expertsforge.com Submit Article My Expertsforge
 
RSS Feeds, Help Help RSS Feeds
bannertop
 

Ajax Tutorial: Including external page into current document using AJAX

jawahar
12/1/2005 2:56:42 AM, Views: 1501
This script uses Ajax (DHTML interacting with the server) to let you dynamically include the contents of an external page onto the current document.

Step 1: Insert the below script to the HEAD section of your page:

<script type="text/javascript">
//To include a page, invoke ajaxinclude("afile.htm") in the BODY of page
//Included file MUST be from the same domain as the page displaying it.

var rootdomain="http://"+window.location.hostname

function ajaxinclude(url) {
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.open('GET', url, false) //get page synchronously
page_request.send(null)
writecontent(page_request)
}

function writecontent(page_request){
if (window.location.href.indexOf("http")==-1 || page_request.status==200)
document.write(page_request.responseText)
}

</script>



Step 2 : Once that's done, to include an external page, simply use the below code in the BODY section of the master page where you want its contents to be shown:

<script type="text/javascript">
ajaxinclude("afile.htm")
</script>


This will cause the script to retrieve "afile.htm" and display its content on the page. You can call the above multiple times with different file names to include multiple files. Note that the included file MUST be from the same domain as the page including it due to security limitations with this feature.

You can also include the file by specifying the full URL to it on your server, such as:

<script type="text/javascript">
ajaxinclude(rootdomain+"/includes/afile.htm")
</script>


where the part in bold ("/includes/afile.htm") is the path to your external file MINUS your domain name itself (ie: http://www.expertsforge.com). Do not specify your domain, as the script will detect that by itself via the variable "rootdomain." Again, the limitation with both the included file and master page being on the same domain applies.
Next Steps:
Add this Tutorial to:
Blink Blink del.icio.ous Del.icio.us Digg Digg
Fark Fark Furl Furl Google Google
Reddit Reddit Simpy Simpy Spurl Spurl
Technorati Technorati Windows Live Win Live Yahoo Yahoo
Rate Me!
Not Yet Rated!
Rate:
Send Private MessageSend Message
Signup / Login To View the Solution or Provide Comments
Post Comment/Solution
Comment:*
        (Link Rules) 
  Use : [bold] for <b>; [/bold] for </b>; [italic] for <i>; [/italic] for </i>; [code] & [/code] for code
 
Categories
Options
Ajax RSS Feed
Most Popular Tutorial
Most Popular Solution
No Records!
Top Rated
No Records!
Top Rankers
Overall
1. jawahar (150)
2. francis_wong (50)
Yearly -2008
No Rankings!
Expertsforge Sponsors
bnrtop