0

I made a fully functional Ajax Content Replacement script. The problem is that it adds forwards like /#about or /#work or /#contact to the adress but when I reload the site, the main page will be show. Why? How is it possible that when i type in the adress the right subpage will be show?

Someone told me that the problem is that I added the file manually when I use popstate. So I want a solution without popstate. I am not a Javascript expert but I would like to learn it. Because popstate but this is very circuitous.

window.location.hash = $(this).attr('href'); 

My .html files are in stored in /data/. The strange thing is that it finds the file but when I try to find it manually,the page show the main page or when I refresh the site with F5 the main page will be show,too.

Can you help me and show me how it works. We can use my code to find the error. Thanks a lot.

Here is the Websitelink : Demo Link

function refreshContent() {
var targetPage = 'home';
var hashMatch = /^#(.+)/.exec(location.hash);
// if a target page is provided in the location hash
if (hashMatch) {
  targetPage = hashMatch[1];
}
$('#allcontent').load('data/' + targetPage + '.html');
}


$(document).ready(function(){
refreshContent();
window.addEventListener('hashchange', refreshContent, false);
$('.hovers').click(function() {
var page = $(this).attr('href'); 
$('#allcontent').fadeOut('slow', function() {
$(this).animate({ scrollTop: 0 }, 0);
$(this).hide().load('data/' + page +'.html').fadeIn('normal');
});

});
});

$('.hovers').click(function() {
  window.location.hash = $(this).attr('href'); 
  $.get('data/'+this.href, function(data) {
    $('#allcontent').slideTo(data)      
  })
  return false  
})
martin85
  • 35
  • 6

1 Answers1

2

You should load the initial page based on location.hash (if provided) on page load:

function refreshContent() {
   var targetPage = 'home';
   var hashMatch = /^#!\/(.+)/.exec(location.hash);
   // if a target page is provided in the location hash
   if (hashMatch) {
      targetPage = hashMatch[1];
   }
   $('#allcontent').load('data/' + targetPage + '.html');
}


$(document).ready(function(){
   refreshContent();
   ...

You can make back and forward work by listening to the Window.onhashchange event:

window.addEventListener('hashchange', refreshContent, false);

Do note that this doesn't work in Internet Explore 7 or lower.

Edit:

Okay, try this:

var $contentLinks = null;
var contentLoaded = false;

function refreshContent() {
   var targetPage = 'home';
   var hashMatch = /^#(.+)/.exec(location.hash);
   var $content = $('#allcontent');

   // if a target page is provided in the location hash
   if (hashMatch) {
      targetPage = hashMatch[1];
   }

   // remove currently active links
   $contentLinks.find('.active').removeClass('active');
   // find new active link
   var $activeLink = $contentLinks.siblings('[href="' + targetPage + '"]').find('.navpoint');
   // add active class to active link
   $activeLink.addClass('active');
   // update document title based on the text of the new active link
   window.document.title = $activeLink.length ? $activeLink.text() + ' | Celebrate You' : 'Celebrate You';

   // only perform animations are the content has loaded
   if (contentLoaded) {
       $content
           .fadeOut('slow')
           .animate({ scrollTop: 0 }, 0)
       ;
   }

   // after the content animations are done, load the content
   $content.queue(function() {
       $content.load('data/' + targetPage + '.html', function() {
           $content.dequeue();
       });
   });

   if (contentLoaded) {
       $content.fadeIn();
   }

   contentLoaded = true;
}

$(document).ready(function() {
    $contentLinks = $('.hovers');

    refreshContent();

    window.addEventListener('hashchange', refreshContent, false);

    $contentLinks.click(function(e) {
        e.preventDefault();
        window.location.hash = '!/' + $(this).attr('href'); 
    });
});
sroes
  • 14,663
  • 1
  • 53
  • 72
  • My bad, `$(window).hashchange` doesn't exists because it isn't supported in every browser. I updated the code to `window.addEventListener('hashchange', refreshContent, false);`. – sroes Jan 20 '14 at 15:21
  • please have a look at this http://celebrateyou.eu/... the fade is broken ... i have the acutally code above refreshed. – martin85 Jan 20 '14 at 15:27
  • I wasn't able to test the code and forgot 2 things. Try again now. – sroes Jan 20 '14 at 20:26
  • load.js doesn't seem to be modified with the last changes – sroes Jan 20 '14 at 20:32
  • oh my god! its working so nice!! :) Look at it its uploaded. working so nice! :) Thank you soooooo much for your time and help! :) – martin85 Jan 20 '14 at 20:39
  • one last question: is it possible to delete the #tag from the url? for example: example.com/#contact to example.com/contact – martin85 Jan 20 '14 at 20:41
  • Do you know how to debug your Javascript? Knowing how to use tools like Chrome Developer Tools or Firebug will also help you learn. – sroes Jan 21 '14 at 07:14
  • yes of course but how can kelp firebug me to solve the problem with the #contact for example... is this not a htaccess problem – martin85 Jan 21 '14 at 08:47
  • Well, if you want to use `/contact` instead of `/#contact` you would need to use [history.pushState](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history) instead of the hash, which is totally different technique. There are already existing scripts for this, have a look at https://github.com/defunkt/jquery-pjax – sroes Jan 21 '14 at 09:41
  • ok i've added a active class for the navigation but with the forwards and backwards its not working in coop with this code. Please have a look at http://celebrateyou.eu – martin85 Jan 21 '14 at 12:12
  • Yeah, you should find the link with the target page in `refreshContent`. See my edit. – sroes Jan 21 '14 at 12:41
  • updated @url :) Thank you so much! Is there any chance maybe to manipulate the url like this /#about to /#!/about with this technique? or is it not possible? Anyway thanks a lot! – martin85 Jan 21 '14 at 12:49
  • Sure, you'd have to change 2 rows: `var hashMatch = /^#(.+)/.exec(location.hash);` and `window.location.hash = $(this).attr('href');`. I'll leave that up to you as an exercise :) – sroes Jan 21 '14 at 12:57
  • i would say the first line must change to var hashMatch = /^#!\/(.+)/.exec(location.hash); but the second line i haven't any idea – martin85 Jan 21 '14 at 13:06
  • Correct, the second line: `window.location.hash = '!/' + $(this).attr('href');` – sroes Jan 21 '14 at 13:09
  • Nice! working fine! One last Question :) Is it possible to change the url title to the specific page? Like this Home | Celebrate You to About | Celebrate You ... for example? Is this a solution way? addthis.update('title', window.location.href); – martin85 Jan 21 '14 at 13:36
  • Okay, last update. It now updates the document title based on the text of the active link – sroes Jan 21 '14 at 13:51
  • oh :/ i dont delete the cache .. the navigation active mode is not longer working. why? :( And when i link to a new site (click on the red div on home) the link doesn't work and send me for example to /about not to /#!/about I've updated it on http://celebrateyou.eu – martin85 Jan 21 '14 at 14:45
  • I accidentally removed `$activeLink.addClass('active');` – sroes Jan 21 '14 at 14:54