javascript - JQuery History.js plugin not replacing state of one page in both HTML4 and HTML5 browsers -
i using jquery history.js plugin enable history api in html5 browsers , emulate in html4 browsers. using ajaxify script implement plugin. changed script little shown:
var history, $, document; function preparevariables() { history = window.history, $ = window.jquery, document = window.document; } function inithistory() { // prepare variables var /* application specific variables */ //contentselector = '#content,article:first,.article:first,.post:first', contentselector = '#navcontent'; $content = $(contentselector), //.filter(':first'), //contentnode = $content.get(0), $menu = $('#menu,#nav,nav:first,.nav:first').filter(':first'), activeclass = 'active selected current youarehere', activeselector = '.active,.selected,.current,.youarehere', menuchildrenselector = '> li,> ul > li', completedeventname = 'statechangecomplete', /* application generic variables */ $window = $(window), $body = $(document.body), rooturl = history.getrooturl(), scrolloptions = { duration: 800, easing: 'swing' }; // ensure content if ($content.length === 0) { $content = $body; } // internal helper $.expr[':'].internal = function (obj, index, meta, stack) { // prepare var $this = $(obj), url = $this.attr('href') || '', isinternallink; // check link isinternallink = url.substring(0, rooturl.length) === rooturl || url.indexof(':') === -1; // ignore or keep return isinternallink; }; // html helper var documenthtml = function (html) { // prepare var result = string(html) .replace(/<\!doctype[^>]*>/i, '') .replace(/<(html|head|body|title|meta|script)([\s\>])/gi, '<div class="document-$1"$2') .replace(/<\/(html|head|body|title|meta|script)\>/gi, '</div>'); // return return $.trim(result); }; // ajaxify helper $.fn.ajaxify = function () { // prepare var $this = $(this); // ajaxify //$this.find('a:internal:not(.no-ajaxy)').click(function (event) { $this.find("a[data-isnav='0']").click(function (event) { // prepare var $this = $(this), url = $this.attr('href'), title = ($this.attr('title') || null); // continue normal cmd clicks etc if (event.which == 2 || event.metakey) { return true; } // ajaxify link history.pushstate(null, title, url); event.preventdefault(); return false; }); // chain return $this; }; // ajaxify our internal links $body.ajaxify(); // hook state changes $window.bind('statechange', function () { // prepare variables var state = history.getstate(), url = state.url, relativeurl = url.replace(rooturl, ''); // start fade out // animating opacity 0 still keeps element's height intact // prevents annoying pop bang issue when loading in new content $content.animate({ opacity: 0 }, 800); // ajax request traditional page callajax("getcontent", { url: url /*typeofheader: contenttype, argsdata: argdata*/ }, false, function () { var ops = $('#ops'); if (ops != null) ops.html(''); showprogress(''); //var = (new date()).gettime(); //caching //if (headercache.exist(url)) { // tdiff = - headercachetime; // if (tdiff < 3000) { // setcontentdata(headercache.get(url)); // return true; // } //} }, function (d) { //headercache.set(url, d, null); //cachename = url; hideprogress(); setcontentdata(d); }, null); // end ajax }); // end onstatechange } (function (window, undefined) { // prepare our variables preparevariables(); // check see if history.js enabled our browser if (!history.enabled) { return false; } // wait document $(function () { inithistory(); }); // end ondomload })(window); // end closure function updatehistory() { var title = (document.title.trim().length > 0 ? document.title : null); var url = window.location.href.replace(/^.*\/\/[^\/]+/, ''); var history = window.history; history.replacestate(null, title, url); $('a[data-isnav="0"').click(function () { // prepare var $this = $(this), url = $this.attr('href'), title = ($this.attr('title') || null); // continue normal cmd clicks etc if (event.which == 2 || event.metakey) { return true; } // ajaxify link history.pushstate(null, title, url); event.preventdefault(); return false; }); } function setcontentdata(d) { var data = d.data; // fetch scripts //$scripts = $datacontent.find('.document-script'); //if ($scripts.length) { // $scripts.detach(); //} // fetch content contenthtml = data; if (!contenthtml) { document.location.href = url; return false; } // update menu //$menuchildren = $menu.find(menuchildrenselector); //$menuchildren.filter(activeselector).removeclass(activeclass); //$menuchildren = $menuchildren.has('a[href^="' + relativeurl + '"],a[href^="/' + relativeurl + '"],a[href^="' + url + '"]'); //if ($menuchildren.length === 1) { $menuchildren.addclass(activeclass); } // update content $content.stop(true, true); $content.html(contenthtml).ajaxify().css('opacity', 100).show(); /* fade in here if you'd */ //intialize other content initcontent(); // update title //document.title = $data.find('.document-title:first').text(); //try { // document.getelementsbytagname('title')[0].innerhtml = document.title.replace('<', '<').replace('>', '>').replace(' & ', ' & '); //} //catch (exception) { } // add scripts //$scripts.each(function () { // var $script = $(this), scripttext = $script.text(), scriptnode = document.createelement('script'); // if ($script.attr('src')) { // if (!$script[0].async) { scriptnode.async = false; } // scriptnode.src = $script.attr('src'); // } // scriptnode.appendchild(document.createtextnode(scripttext)); // contentnode.appendchild(scriptnode); //}); // complete change if ($body.scrollto || false) { $body.scrollto(scrolloptions); } /* http://balupton.com/projects/jquery-scrollto */ $window.trigger(completedeventname); // inform google analytics of change if (typeof window._gaq !== 'undefined') { window._gaq.push(['_trackpageview', relativeurl]); } // inform reinvigorate of state change if (typeof window.reinvigorate !== 'undefined' && typeof window.reinvigorate.ajax_track !== 'undefined') { reinvigorate.ajax_track(url); // ^ use full url here reinvigorate supports } }
it working fine , content added on page using ajax added previous state using updatehistory()
function. on pages state updated on 1 page not updating content when page accessed second time. searched similar questions unable solution. first thought problem internet explorer tried on firefox didn't work. please tell me can reason?
update
it's working urls like:
but not for:
it's first page not saved. try call updatehistory()
or history.pushstate(null, title, url)
inside inithistory()
.
Comments
Post a Comment