jQuery BBQ, deep-linking HREF's with a set Class --- Possible?
Currently, my understanding of jQuery BBQ is it requires me to use an href like href="#/stufff/123/13"
Problem with that is it requires me to add a # to all my URLs through开发者_如何学运维out the site which then means non-javascript browsers can't use the site...
My question, is there a way I can simply add a class to all HREFs I want deep linked? something like class="deep-link-it-please".... Ideas? It also creates a headache in Rails which uses link_to which does not support adding a hash # before the URL.. thanks
What about adding a hash to the links you want to hijax?
$('.bbq-nav a').each(function(){
var nice = $(this).attr('href');
$(this).attr('href','#' + nice);
});
You shouldn't need to add the hash to your urls if you plan on hijacking them anyway. With normal links inside of my div#container and everything seems to be working fine using this implementation:
$("#container a").live('click', function(){
var hrefRequested = $(this).attr( "href" );
// Push this URL "state" onto the history hash.
$.bbq.pushState({ url: hrefRequested });
// Prevent the default click behavior.
return false;
});
// Bind a callback that executes when document.location.hash changes.
$(window).bind( "hashchange", function(e) {
var url = $.bbq.getState( "url" );
// Used to make sure that when you back up to the page you came in on
// you don't get an empty page.
if ( url == undefined ) { url = window.location.href; }
$('#content-container').load(url + ' #container', function(response, status, xhr) {
if (status == "success") {
console.log('hooray!');
// document.title = ;
// document.location.href = hrefRequested;
// return false;
}
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
// You probably want to actually do something useful here..
});
// Since the event is only triggered when the hash changes, we need
// to trigger the event now, to handle the hash the page may have
// loaded with.
$(window).trigger("hashchange");
精彩评论