RCNeil
RCNeil

Reputation: 8759

Test to see if page loaded with JQuery load()

I've been using JQuery's load(); function a bit to load parts of a site's content.

I've been reading that while this may look nice, using load() ruins any good hope for SEO, so suggestions have been to keep a real link within the ahref link selector I am using, so when Google goes to crawl that link, it will find that content.

Then, I've just been ignoring that action using JQuery's preventDefault();.

My question is, say I have something like

$('#mylink').click(function(a) {
   a.preventDefault();
   $('myContainer').load('about.php');
});

That file, about.php, wouldn't normally include a header or footer because I'm placing it within some container already, BUT, can I have a condition or test of sorts on about.php to see if it was called using load();? So that if someone were to say visit www.mysite.com/about.php it would load the rest of the content in, making it look like a normal page?

Upvotes: 0

Views: 144

Answers (3)

gen_Eric
gen_Eric

Reputation: 227200

On my sites, I like to make it easy, so I always have the header/footer added to the page. When you use jQuery's .load(), you can tell it to parse out a certain part of the loaded page.

$('myContainer').load('about.php #myContainer');

This tells jQuery to load about.php, but only append #myContainer to the element.

Docs: http://api.jquery.com/load/#loading-page-fragments

Upvotes: 1

PlantTheIdea
PlantTheIdea

Reputation: 16359

I do this all the time to make it work with pushState(). Put the contents you are trying to load into a subfolder, like pages/whatever.php, then have the normal page contain all the content u want (header, footer, etc) and require pages/whatever.php. For the jQuery .load(), just pull in the pages/ file.

It's somewhat tedious, but SEO-compliant and much more modular.

Upvotes: 2

adeneo
adeneo

Reputation: 318182

Sure, in your PHP file you can detect if the page was requested with ajax or not, and include the header and footer if it was'nt:

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 
     strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    //requested with ajax
}else{
    //not ajax, include more content
}

You could just as easily make that a variable and use it to trigger PHP includes in the correct places to include more content etc.

Upvotes: 2

Related Questions