I've got quite a dilemma here and would like some suggestions from the community. Here's my problem:
I've created a website that has lots of dynamic content, all content is pulled via AJAX and displayed via JS. The way I'm currently allowing direct links to my content is by modifying the fragment identifier on the fly with JS, here's an example:
http://www.mysite.com/home#/123
Where 123 is the ID of the content. When the content is closed it strips the fragment identifier off again.
This works wonderfully, and when people load the page my AJAX request is successfully requesting the '123' article. But I've just implemented a Facebook like button on all the articles, and now I've hit a brick wall.
Basically, the way Facebook finds what thumbnail and titles and URL to associate with your 'liked' content is to check the meta tags of the URL passed into the fb like iframe. It does this by doing a GET request at the URL, checking the content of the meta tags, and then displaying those on Facebook.
The problem is that fragment identifiers are not passed to the server, so I have no way in PHP (that I'm aware of) to dynamically generate these meta tags with the content from the specifically requested article.
For example, I wanted to be able to do something like this:
$id_vals = get_id_values($hash_fragment);
echo '<meta property="og:title" content="'.$id_vals['title'].'" />';
Your first thought is probably: Why not use a GET request or query string? i.e.:
http://www.mysite.com/home?id=123
However the problem with this approach is that Javascript cannot dynamically change this portion of a URL, it can only change the fragment identifier. This means that all direct links to our articles will require page refreshes. This goes against our whole 'dynamic' approach we have going - which I might add is really nice :)
So I'm kind of stumped at the moment. The only semi-solution I can think of is to use an approach of both:
In the fb like requests use get parameters and for direct links use fragment identifier.
But this poses the following problems:
Sorry for the wall of text! Appreciate any input you can provide.
EDIT: I just tested my proposed solution involving a combination of both, and I can confirm Facebook follows the redirect, so this solution doesn't work at all. This is depressing!
EDIT EDIT: One possible way to implement my 'mix' idea is to use a window.location redirect instead of a PHP header() call redirect, so Facebook fetches the meta tags from the original page, for those of you with a similar problem.
Thanks.
The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document. It is typically used to identify a portion of that document. The generic syntax is specified in RFC 3986. The hash-mark separator in URIs is not part of the fragment identifier.
Fragment identifiers are not sent to the server. The hash fragment is used by the browser to link to elements within the same page.
HTML provides two ways to identify a document fragment: by inserting an anchor ( a ) element with the name attribute (instead of href ) or by adding the id attribute to any HTML element.
A fragment is an internal page reference, sometimes called a named anchor. It usually appears at the end of a URL and begins with a hash (#) character followed by an identifier. It refers to a section within a web page. In HTML documents, the browser looks for an anchor tag with a name attribute matching the fragment.
If you can rejigger your site to use hashbang #!
URLs, Facebook somewhat quietly supports Google's AJAX crawling spec.
On encountering a URL containing #!
, it will automatically convert that and the following text to the GET-friendly query string ?_escaped_fragment_=
. Your server-side code can key off that GET param, if it's present.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With