0

My goal: I have a form that is in parts, 1-4, when the user clicks on the "Next" button I would like the content to animate out then part 2 slides, and so on until the form is complete. The tricky part is I would am trying to use a different php page in a different subfolder to insert as the other 3 parts. This would also change the URL subfolder the user sees.

The working example is actually WordPress. When you click through the multi-part form you will see the content and the URL act as I have described.

I did a bit of digging and it seems like they used React.js on the content but I couldn't really find any documentation on how to do this with React.js so it made me think that maybe it was custom Ajax/jQuery or what.

My Trees of Folders -

  • Main
    • Subfolder-1
      • index.php
    • Subfolder-2
      • index.php

And so on. The only thing I could think of would to use jQuery:

$(document).ready(function() {
  $('#form-container').on('click', '.insert', function() {
    var directory = $(this).attr('name');
    $('#form-container').load('../' + directory);

    return false;
  });
});

I add the class of "insert" on the "Next" button and give it a name="Subfolder-2" $('#form-container').load('../Subfolder-2);' will actually load the content into the div without the page refreshing BUT it does not change the subfolder in the URL.

Am I on the wrong track? Maybe I am just not searching for the right thing?

halfer
  • 19,824
  • 17
  • 99
  • 186
Matthew
  • 922
  • 1
  • 6
  • 21
  • 1
    I think you need to use `window.history.pushState` to add a history entry (and change the URL). You then need to use `window.onpopstate` to handle the user clicking the browser's Back button. See [this answer](http://stackoverflow.com/a/3354511/859640). – John S Jul 16 '16 at 04:39
  • Thank you, i will try your suggestion when i get home tonight. That makes sense though. I never thought of using pushState and onpopstate. – Matthew Jul 16 '16 at 18:08
  • Hey, @JohnS, I wanted to say thank you again for your comment yesterday. After doing quite a bit of research and testing on `history.pushState` and `popstate` I have gotten this to function how I described. If you post your suggestion as an answer I will accept it. Thanks again! You were very helpful – Matthew Jul 18 '16 at 03:37
  • 1
    Matthew, my comment just pointed you in the right direction. You could add your own answer and show some of your code. I'm curious what it looks like. – John S Jul 18 '16 at 15:56

1 Answers1

0

Ok, so I ended up figuring out how to get the content to act like I wanted with the information John S. provided me. After doing some research and a few hours of trial and error I came up with the JavaScript below:

var data = 'start',
    url = '../' + data + '/';
    history.pushState(data, null, url);

Above I set the variables and immediately run a history.pushState on page load to capture the first div that is loaded into the content div. This is important because it is the only way I could load the content that happens on initial load back into the page when hitting the browsers back button.

$('body').on('click', '.insert', function(e) {

        data = $(this).attr('data-name'),
        url = '../' + data + '/';

        history.pushState(data, null, url);

    request_content(data);

    return false;

});

Then I add a click listener to the button with the class .insert reset the variables so instead of grabbing the page that initially loaded it grabs the page that will be loaded, then use history.pushState again to change the url that is determined by the variables.

The request_content function is a simple .load function. So when the button is clicked the variables are set, the url changes and the new content get loaded into the page while the old content disappears.

The final piece to the puzzle which took me the longest to figure out is actually the popstate function. I am still not 100% sure why it works but after hours of messing with it and finally getting it to work I am not going to question it.

window.addEventListener('popstate', function(e){
    var data = e.state;

    if(data === null) {

    } else {
        request_content(data);
    }
});

This popstate function is what allows the content to come back when hitting the browsers back or forward navigation.

CSSTricks < this article at CSSTricks helped a TON when learning this method.

Thanks again to John S. for pointing me in the right direction!

Matthew
  • 922
  • 1
  • 6
  • 21