If you are building rich web applications, then you will obviously require some ajax to give your users a smooth browsing experience. And jQuery makes it easier for you to do that without any trouble.
In my previous articles, I have shown you how to toggle a div element and also changing its content. Now I will show you how to load content from an external file into a div element.
loadContent() function
Though most of the jQuery code is placed inside head tag, I prefer that you create an additional javascript function for managing inline anchor links quickly.
- function loadContent(elementSelector, sourceUrl) {
- $(""+elementSelector+"").load("http://yoursite.com/"+sourceURL+"");
- }
This is the main jQuery code for loading remote content inside an element
- $("#content").load("http://yoursite.com/source.php");
Usage
- <a href="javascript:loadContent('#content', 'source_page.php');">Link 1</a>
- <div id="content">content will be loaded here</div>