1

My website structure is a header, content, footer. My goal is that every time the visitor clicks on the links, only the "content" part loads, everything else stays the same.

I don't know what is the best practice to reach this effect I read a lot on the web, I found some examples for iFrames and or jquery/css/ajax, but I can't make up my mind what to use. (The content on the website, that would be changing, would be all sub pages of my website, hence I thought iFrames is not the tool I need, but correct me if I am wrong.)

The "content" of the page consist of a white background and a layer above it, that are pictures / text.

What I want to achieve is whenever the visitor clicks a link, the content layer of text / pictures "fades out", the white background re sizes to a new size of the sub page, then the new content of the sub page fades in.

The "fade in, fade out" is implemented already:

$(document).ready(function() {
$("#valtozo").css("display", "none");
$("#valtozo").fadeIn(2000);
$("a").click(function(event){
    event.preventDefault();
    linkLocation = this.href;
    $("#valtozo").fadeOut(1000, redirectPage);
});
function redirectPage() {
    window.location = linkLocation;
}

});

"valtozo" is the id of the changing div.

For the smooth re-size of the transition of the white background I thought I use this: jQuery Animation - Smooth Size Transition

I would like to hear your thoughts / ideas what to use to achieve frames like functionality while keeping all visual effects, mentioned above.

Sorry for long post, thanks in advance.

(Btw if u have better ideas, more compact or better, fire away, nothing is set in stone!)

Community
  • 1
  • 1
Gergely Csata
  • 65
  • 2
  • 11
  • One thing to be aware of with this kind of site functionality is that it is not very good at all for SEO. If you need to optimise your site for high ranking in Google then this is not the best way forward. – Rory McCrossan Feb 05 '12 at 13:58
  • I know that, SEO is important, what would you recommend then? – Gergely Csata Feb 05 '12 at 21:53

2 Answers2

0

You can use defunkt's pjax.

pjax loads HTML from your server into the current page without a full reload. It's ajax with real permalinks, page titles, and a working back button that fully degrades.

You will need to implement a server-side component that send you only the content of the page, without all the menus and stuff. Then it'll work.

Sample code

Not fully tested, please comment whether or not it works. To test it, make sure it's done over HTTP (not file://) - at least when using Chrome.

Check the jsFiddle example with a fake request and alerts showing triggered events.

In index.html

<html>
  <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" src="https://raw.github.com/defunkt/jquery-pjax/master/jquery.pjax.js"></script>
    <script type="text/javascript" src="pjax-fade-click.js"></script>
  </head>

  <body>
    <div id="valtozo">
      This will be replaced.
    </div>

    <a href="news.html" data-pjax="#valtozo">Show news</a>
  </body>
</html>

In news.html (should check for pjax to enable/disable menus)

<h1>News!</h1>

In your pjax-fade-click.js

$('a[data-pjax]').pjax();

$('body')
  .bind('pjax:start', function(event, xhr, options) { $(options.container).fadeOut("fast"); })
  .bind('pjax:end',   function(event, xhr, options) { $(options.container).fadeIn("fast"); });

Then create all your html pages as you normally would, but don't show menus when you receive the header 'X-PJAX' or querystring _pjax=true.

Edit 1

Didn't double check the pjax version on the pjax demo site - turns out it was old. Now using the raw version of jquery.pjax.js straight from github, but it should be loaded from somewhere else. Also added the jsfiddle.

Community
  • 1
  • 1
Joel Purra
  • 24,294
  • 8
  • 60
  • 60
  • Sorry if it took long time to respond, but I had my hands full. I tried your code, the part with ajax works, the Fade-in Fade-out does not. Any ideas why not? – Gergely Csata Feb 20 '12 at 13:28
  • @GergelyCsata: Hmm, yes, I investigated it a little bit. It seems as if the pjax demo didn't run the latest code, so the events were never triggered. Whaddayaknow. Plus, mistook the returned options for pjax options, when they were ajax options. Updated the code. Also, fading in and out may take too much time on a small/fast page =) – Joel Purra Feb 20 '12 at 15:14
  • I copy pasted your new .js code, to mine, and while I see it work on Fiddle, it does not work on my site. A., it kills the original functionallity, it does not load in the valtozo div, the link reload like a new html page B., its not fading :D – Gergely Csata Feb 20 '12 at 15:41
  • I would suggest re-copying all code to new files, downloading `jquery.pjax.js`, and testing them on your own webserver. One small step at a time. Also, note that `news.html` cannot contain `` when pjax requests it, since pjax will ignore the result (and just go to the address). – Joel Purra Feb 20 '12 at 15:59
  • ---EDIT 1--- I re-copied everything, functionality works, no fading still... ---EDIT 1--- I removed (ran into that problem earlier :D) I will re-download everything then. I am testing everything on my Ubuntu webserver, so I don't think thats the problem. I will test them now, will get back to you in minutes – Gergely Csata Feb 20 '12 at 16:40
  • Did you keep the `alert()` boxes so you can confirm that the fade routines are running on your server? If you don't *see* fading, it might be because the request is too fast to see a fade - that is why I put the alerts in. – Joel Purra Feb 20 '12 at 18:40
  • Something is missing here, when the script is operating outside of Fiddle. I downloaded the example html code from there, with the example Javascript. I downloaded all files, changed paths to the local files. Locally it does not work, but as I see it on fiddle the javascript we are testing is not actually linked in. Isn't something we should be doing differently? I am just asking since I have no idea what goes wrong... – Gergely Csata Feb 20 '12 at 20:20
  • Yes, jsFiddle runs the code in the javascript pane at `onLoad` by default. Try wrapping the pjax init so it [runs when the page/jQuery is ready](http://api.jquery.com/ready/), like `$(function () { /* $('a[...).pjax() ... and more */ });` – Joel Purra Feb 21 '12 at 06:39
0

I'm assuming you're looking to only reload a specific section of the page and not the entire page right? If so, this is how you can go about it:

STEP 1: Create a blank / empty container for the reloadable section in question. Your index.html file should look something like this:

<div id="wrapper">

   <!--menu-->
   <a class="section-1">Section 1 </a>
   <a class="section-2">Section 2 </a>
   <a class="section-3">Section 3</a>
   <!--menu-->

   <!--reloadable-section-->
   <div id="reload">

   </div>
   <!--reloadable-section-->

</div>

STEP 2: Create the necessary reloadable sections in separate HTML file(s) and save them where you deem fit. For the purpose of this example, they will be saved in the same directory as the index.html file. So your section-1.html, section-2.html and section-3.html files will contain all the necessary info / content. Note that if you are using Dreamweaver to create the HTML file, the HTML markup tags, BODY and HEAD tags will automatically be inserted for you - remove those tags as you do not need them in this instance. Instead, immediately create the divs needed to hold your content. Your file should look something like this:

<div id="section-1">

    <h2>this is section-1</h2>

    <p>this is the content for section 1</p>

</div>

STEP 3: Go back to your index.html file and include the following jQuery:

    <script>

        $(document).ready(function(){
            $('.section-1').click(function(){
                 $('#reload').html('<div class="loading"><img src="images/loading.gif"/></div>'); //this is optional; it's just to pull in a loading spinner gif to indicate that content is loading so you can leave it out if you want to.
                 $('#reload').load('section-1.html');
            });
            $('.section-2').click(function(){
                 $('#reload').html('<div class="loading"><img src="images/loading.gif"/></div>'); //this is optional; it's just to pull in a loading spinner gif to indicate that content is loading so you can leave it out if you want to.
                 $('#reload').load('section-2.html');
            });
            $('.section-3').click(function(){
                 $('#reload').html('<div class="loading"><img src="images/loading.gif"/></div>'); //this is optional; it's just to pull in a loading spinner gif to indicate that content is loading so you can leave it out if you want to.
                 $('#reload').load('section-3.html');
            });
        });

    </script>

So just by doing that, you'd be able to load in the right file into a specified container of your HTML file without reloading the entire HTML file. Do note that if you wish to have the section-1.html have the effect of fading in upon load, this is what you can do:

<script>

    $('#section-1').fadeTo(500,1);

</script>

<div id="section-1">

    <h2>this is section-1</h2>

    <p>this is the content for section 1</p>

</div>

Yup, it's as simple as that - just add the mentioned jQuery like above and the content container in the loadable files will fade in on document ready. Of course, remember to set the specified div's opacity to 0 via CSS. EDIT: Note that for this instance you do not need "$(document).ready(function(){});" at all.

From personal experience, I would generally refrain from using iFrames unless it is absolutely necessary to do so. This is simply because there are quite a few stylistic complications with iFrames that can be quite a nightmare, especially if the parent div's height is to be inherited from the amount of child content residing within it.

PS: I'm not too sure if this method can be strictly labelled as AJAX loading, but I'm reserving it for those who know better to shed some light on that. Sorry, I'm just a self taught programmer so my knowledge is quite limited.

vynx
  • 1,269
  • 1
  • 12
  • 20
  • Hi scornork! Thank your for your answer, later today (few hrs) I will implement it, then I will get back to you, but it looks very promising. P.S.: I am using notepad++, that does not insert html/body tags, or does it? – Gergely Csata Feb 05 '12 at 15:20
  • Hey gergely, yeah.. notepadd++ doesn't usually include those tags, but keep an eye out and remove them from the files to be loaded in otherwise they might likely screw with the parent index.html file. all the best! hope this works for you! – vynx Feb 05 '12 at 15:22
  • I got delay, due other things I had to do today, getting to your work at this minute :D – Gergely Csata Feb 05 '12 at 21:18
  • Scornok, I implemented everything you wrote, ctrl c ctrl v style. The thing is on the main page I have a Section 1, Section 2, Section 3. But its plain text, I presume u forgot the href part from the ? – Gergely Csata Feb 05 '12 at 21:26
  • Sorry for the comments coming after on another, I figured your code out. Quite handy I must say it. My problem is that the fading does not work.. :( adding this to section-1.html has no effect: – Gergely Csata Feb 05 '12 at 21:36
  • hey gergely, apologies. exclude "$(document).ready(function(){});" entirely. i've amended the answer for your reference. – vynx Feb 06 '12 at 04:10
  • hi gergely, would appreciate if you could accept this as an answer as well if it has somehow helped. – vynx Mar 20 '12 at 22:42