Load whole page in AJAX - ajax

Currently, I have a wordpress website. I want to load the whole page of the website. I am aiming to put a footer player on the website, and that will only work if the rest of the pages are loaded with AJAX.
The goal is to click on the link of for example an article, and that then the website with the article will load. Because the lay-out of the article page differ from the homepage, just loading one div is not enough (see http://newtheme.favoritefm.com).
I have tried this:
$.fn.initLinks = function() {
$("a",this).click(function() {
var url = $(this).attr("href");
// transition to loading phase ...
// Ajax post parameter tells the site lo load only the content without header/footer
$.post(href,"ajax=1",function(data) {
$("#content").html(data).initLinks();
// transition to normal phase ...
});
return false;
});
};
$(function() {
$("body").initLinks();
});
Doesn't do anything. What to do?

Use the full jQuery Ajax Method: $.post instead of $.post.
So your code should look like this:
$.fn.initLinks = function() {
$("a", this).click(function() {
var url = $(this).attr("href");
// transition to loading phase ...
// Ajax post parameter tells the site lo load only the content without header/footer
$.ajax({
url: url // your href attr..
success: function(data) {
$("#content").html($(data).filter('#content').html());
// This fills in the #content on the current page with the html in #content from
// the Request page
},
error: function() {
alert('Could not load Data!!');
}
});
return false;
});
};
$(function() {
$("body").initLinks();
});

Related

Ajax loaded content div class is not applied

I have a page that has a few divs connected in a flowchart via JS-Graph.It. When you click one of the divs, I want it to 1) generate text in a special div 2) generate a popup via click functions attached to the two classes "block" and "channel" in each div. This works when the page is static.
When I add ajax so on click of a button and add more divs, only one of the two classes appears in the HTML source. "Channel" is no longer visible and the function to generate a pop-up on click of a channel class div does not work anymore...
AJAX call:
$("#trace").bind('click', $.proxy(function(event) {
var button2 = $('#combo').val();
if(button2 == 'default') {
var trans = 'Default View';
}
if(button2 == 'abc') {
var trans = 'abc';
}
$.ajax({ // ajax call starts
url: 'serverside.php', // JQuery loads serverside.php
data: 'button2=' + $('#combo').val(), // Send value of the clicked button
dataType: 'json', // Choosing a JSON datatype
success: function(data) // Variable data constains the data we get from serverside
{
JSGraphIt.delCanvas('mainCanvas');
$('.test').html('<h1>' + trans + '</h1>'); // Clear #content div
$('#mainCanvas').html(''); // Clear #content div
$('#mainCanvas').append(data);
JSGraphIt.initPageObjects();
}
});
return false; // keeps the page from not refreshing
}, this));
DIV class: (works in index.php but not transactions.php)
// Boxes
while($row = sqlsrv_fetch_array($result))
{
echo '<div id="'.$row['id'].'_block" class="block channel" style="background:';
Functions:
$(document).on('click', '.block', $.proxy(function(event) {
var input = $(event.target).attr('id');
var lines = input.split('_');
var button = lines[0];
$.ajax({
url: 'srv.php',
data: 'button=' + button,
dataType: 'json',
success: function(data)
{
$('#content').html('');
$('#content').append(data);
}
});
return false;
}, this)); // End Application Details
$(".channel").click(function () {
alert('channel');
});
Something about registering with pages, I'm not sure exactly how it works. The fix should be to change your channel click function to be the same as your first and use the .on('click') option.
found some related reading material. https://learn.jquery.com/events/event-delegation/

Loading Content Via Ajax

Ok, so I'm pretty new to ajax and loading content externally and would appreciate any insight to my problem.
I currently have a hidden div that is empty where the ajax content should load in after a link is clicked.
<div id="#ajax-wrap"></div>
I currently have a list of links that all have the same class and I'd like to have, when clicked, the blank div do a slide toggle and then load in the content from the page that the link was to.
Link:
Current jQuery:
$("a.home-right").click(function () {
$('#ajax-wrap').slideToggle();
});
Being as I'm new to Ajax and loading the external content, I'd like to know how to load content from the linked page that's house in the #content tag. So essentially, I'd like a .home-right link, #ajax-wrap would slide toggle, and then Ajax would pull content from the linked page (which is always going to be a different random link) and it's #content div, placing that content in #ajax-wrap.
Thanks in advance for any help folks!
You want to set the ajax for the links. Requirements:
Writing a handler for links.
We must cancel the default behaviour of browser when somebody click on a link (that redirect to the page).
Removing old data ajax recieved from server and make the #ajax-wrap fresh.
Load the remote page via ajax and set it to #ajax-wrap.
Now slide it down.
// Document Ready
$(function () {
// attaching click handler to links
$("a.home-right").click(function (e) {
// cancel the default behaviour
e.preventDefault();
// get the address of the link
var href = $(this).attr('href');
// getting the desired element for working with it later
var $wrap = $('#ajax-wrap');
$wrap
// removing old data
.html('')
// slide it up
.slideUp()
// load the remote page
.load(href + ' #content', function () {
// now slide it down
$wrap.slideDown();
});
});
});
You can simply use the ajax like this:
$("a.home-right").click(function () {
$.ajax({
type: "get",
url: "YOUR URL HERE",
success: function(data){
$('#ajax-wrap').html(data);
$('#ajax-wrap').slideToggle();
},
error: function(){
alert("An error occured");
}
});
});
try this:
$(function(){
$("a.home-right").click(function () {
$('#ajax-wrap').slideUp();
var url = $(this).attr("href")
$.ajax({
type: "get",
url: url,
success: function(msg){
$('#ajax-wrap').html(msg)
$('#ajax-wrap').slideDown();
},
error: function(){
alert("An error occured");
}
});
})
})
Use get:
// Inside click handler
$.get('url_that_returns_data')
.done(function(response){
var $response = $(response);
$('#ajax-wrap').html($response.find('#content')).slideToggle();
});

jquery tabs not firing after content loaded via ajax

I have an index page, in wich I have jquery.js and jquery.tabs.js included.
this page load content via ajax into a . The content is a set of tabs, their contents and at the bottom of the loaded content I have a call to tabs().
in FF it only works when I load content after refreshing the index page, and if I hit the link to reload the content, it stops working. in IE it's not working at all.
These are my tabs:
<ul id="horz-tabs">
<li>tab1</li>
<li>tab2</li>
<li>tab3</li>
</ul>
then I have the containers following, and at the very end of the page loaded via ajax I have:
<script type="text/javascript"><!--
$('#horz-tabs a').tabs();
//--></script>
My ajax function loads the page properly. I am wondering how I can fix this issue. Thanks for any help or advice.
The ajax loading function is as follow
<script>
function async(target, href) {
var loading = '<div class="loading">Loading</div>';
jQuery.ajax({
url: href,
beforeSend: function() {
$(target).prepend(loading);
},
error: function() {
$(target).html("<span class='error'>Failed</span>");
},
success: function(results) {
$(target).html(results);
}
})
}
</script
Look like you are loading the tab html via ajax, that could be the problem since when your script is running the dom for the tab elements may not be existing.
So you need to have a success callback to the async load method and add execute $('#horz-tabs a').tabs(); in the load success callback.
function async(target, href) {
var loading = '<div class="loading">Loading</div>';
return jQuery.ajax({
url : href,
beforeSend : function() {
$(target).prepend(loading);
},
error : function() {
$(target).html("<span class='error'>Failed</span>");
},
success : function(results) {
$(target).html(results);
}
});
}
Then
async('some-div', 'tab-url').done(function(){
$('#horz-tabs a').tabs();
})

How to know when page has been loaded with references completely after post in jquery ajax

I am using the below to get the whole page when I submit a form, now the page takes time to load as it calls its CSS and JavaScript.
I need to show that data is still being loaded in that page, before anybody uses it again.
I would like to use a modal popup with loading and once the page is loaded completely I will close the popup
How canI achieve that?
$.ajax({
type: "POST",
url: $url,
data: "post=" + post,
// data: form_data,
success: function(data) {
if(data!=""){
$('body').html(data);
...
additonal data:
I have two frames in a window. in the right side frame my page has links which when clicked will post certain data in hidden fields and get the whole right side page with the changed data. now my right side page has links such as
<link rel="Stylesheet" href="abc.css" type="text/css" />
<script src="jquery-1.6.3.min.js"></script>
<link href="jquery-ui.css" rel="stylesheet" type="text/css"/>
These files take time to be retrieved form server once the page is loaded by $('body').html(data); I want these files to be loaded and then let the user do his work
Use ajaxStart and ajaxEnd to show the loading message.
$("#myjaxloader").bind("ajaxStart", function(){
$(this).show();
}).bind("ajaxStop", function(){
$(this).hide();
});
Add the message and image in myajaxloader
You can use blockUI if you want more blocking features.
Since you are applying load to the entire body element, any modal popup display prior to this will be removed ready for the new content.
Therefore, I would suggest loading your content into a variable first by using $.get():
var content;
$.get($url, "post=" + post, function(data){
content = data;
});
This means you can have a loading icon displayed during this process, then load the content into the body which will remove the loading icon:
//show modal popup here
$.get($url, "post=" + post, function(data){
var content;
content = data;
if (content!=null){
$("body").html(content);
}
});
My syntax might not be quite right as I haven't been able to test this, but it should give you an idea of the logic which would work.
$(form).on('submit', function(e) {
e.preventDefault(); // to stop the page from reloading
$.ajax({
type: "POST",
url: $url,
data: "post=" + post, //use form.serialize to send the whole form
beforeSend: function () {
$(modal).show();
}
}).done(function(data) {
if(data!=""){ $('body').html(data); }
)).always(function() {
$(modal).hide();
});
});
You need to use the onload event of the loaded frame. If both your frames are on the same domain, you can do something like this:
navigation window:
$.post(url, data).then(
function() { // success handler
window.errorTimer = setTimeout(showErrorPopup, 10);
}, function() { // error handler
showErrorPopup();
}
);
showLoadingPopup();
content window:
$(function() { // runs after everything is loaded
window.top.otherFrame.clearTimeout(window.top.otherFrame.errorTimer);
window.top.otherFrame.hideLoadingPopup.call(window.top.otherFrame);
});

Ajaxify hyperlink so on success will load content otherwise redirect as default

I have a <a href> hyperlink. I used JQuery so clicking the link will load contents into a div in the current page (i.e. stay in the same page), and it works now.
However I also want that, if the request fails, the link act normally and go to the href url.
I tried e.preventDefault(); and return false; in the success: callback function, but they are not in the correct scope. If I place e.preventDefault() in the calling function, I can't reverse that effect later.
Here is my code:
$('a.more-link').click(function(e){
var postId=$(this).closest('div.post').attr("id").replace(/^post-(.*)$/,'$1');
var postContent=$(this).parent();
$.ajax({
url:"?action=ajax_load_post&id="+postId,
success:function(data){
postContent.html(data);
// Can't access e.preventDefault, nor return false;
},
error:function(data){
}
});
e.preventDefault();
});
Don't worry about the preventDefault(), just redirect the user in the error function like this:
$('a.more-link').click(function(e){
var postId=$(this).closest('div.post').attr("id").replace(/^post-(.*)$/,'$1');
var postContent=$(this).parent();
var _this = $(this);
$.ajax({
url:"?action=ajax_load_post&id="+postId,
success:function(data){
postContent.html(data);
},
error:function(data){
window.location = _this.attr('href');
return false;
}
});
});

Resources