Jump to content

Call Ajax File on Page Load


unemployment

Recommended Posts

I just built a news feed with Ajax and now I want to run the ajax feed on page load.  How do I tell it to grab the ajax file and not the php in the file itself?  Is this done with a javascript event? Should I even load the ajax file on page load or only when I am clicking through tabs to load different versions of my news feed?

 

Ideally I should have this backed up with php. I'm not sure the best way to implement this sort of thing.

Link to comment
Share on other sites

your question is not 100% clear to me, however I would use an onclick event with a dynamic url to load the correct version of the news feed. Also, didnt you just say that you got pagination to work for this?

Link to comment
Share on other sites

your question is not 100% clear to me, however I would use an onclick event with a dynamic url to load the correct version of the news feed. Also, didnt you just say that you got pagination to work for this?

 

Essential I have this news feed and it is on the users homepage... pretty generic... something like facebook.  When that loads, should I load the ajax version or my php version in my ajax file?

 

Would it be fast to load the ajax version?

 

The user isn't clicking on anything, they are simply logging into the site.

 

I do have my ajax running in other onclick events and yes,  I got pagination working for this. 

Link to comment
Share on other sites

$(document).ready(function() {
if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest();
} else {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		$("#news_feed).html(xmlhttp.responseText);
	}
}
xmlhttp.open("GET", "../news_feed.php, true);
xmlhttp.send();	
});

 

This will display the news feed automatically when the page gets loaded, and input what's returned in the div container "news_feed". Pretty self-explanatory, but if you want to use this function to load the data on triggered events, give the function a name and write it into your html.

 

BTW, this uses jQuery, so I'd recommend downloading it before trying to use this code. Hope this helps!

Link to comment
Share on other sites

$(document).ready(function() {
if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest();
} else {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		$("#news_feed).html(xmlhttp.responseText);
	}
}
xmlhttp.open("GET", "../news_feed.php, true);
xmlhttp.send();	
});

 

This will display the news feed automatically when the page gets loaded, and input what's returned in the div container "news_feed". Pretty self-explanatory, but if you want to use this function to load the data on triggered events, give the function a name and write it into your html.

 

BTW, this uses jQuery, so I'd recommend downloading it before trying to use this code. Hope this helps!

only problem with this is, the page will need to be loaded before the news feed loads. Could cause slow loading time if there is a lot of content on the page.

Link to comment
Share on other sites

$(document).ready(function() {
if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest();
} else {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		$("#news_feed).html(xmlhttp.responseText);
	}
}
xmlhttp.open("GET", "../news_feed.php, true);
xmlhttp.send();	
});

 

This will display the news feed automatically when the page gets loaded, and input what's returned in the div container "news_feed". Pretty self-explanatory, but if you want to use this function to load the data on triggered events, give the function a name and write it into your html.

 

BTW, this uses jQuery, so I'd recommend downloading it before trying to use this code. Hope this helps!

only problem with this is, the page will need to be loaded before the news feed loads. Could cause slow loading time if there is a lot of content on the page.

 

I'll just go the php route to start. thanks

Link to comment
Share on other sites

$(document).ready(function() {
if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest();
} else {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		$("#news_feed).html(xmlhttp.responseText);
	}
}
xmlhttp.open("GET", "../news_feed.php, true);
xmlhttp.send();	
});

 

This will display the news feed automatically when the page gets loaded, and input what's returned in the div container "news_feed". Pretty self-explanatory, but if you want to use this function to load the data on triggered events, give the function a name and write it into your html.

 

BTW, this uses jQuery, so I'd recommend downloading it before trying to use this code. Hope this helps!

only problem with this is, the page will need to be loaded before the news feed loads. Could cause slow loading time if there is a lot of content on the page.

 

I'll just go the php route to start. thanks

no problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.