0

According to this Question that has been Answered: how-do-soundcloud-keep-music-playing-on-when-navigating

Soundcloud uses a new API that has been introduced by HTML5 - the HTML5 History API.

So I'm wondering, how do implement this on my website (music service website) so I have music playing seamlessly while navigating. I already have a website that plays music (vis1.php - is the music player) when navigating seamlessly by using AJAX to load the content into a modulewrapper. However big down side to this, is that it's not practical at all, especially if there are going to be many users, so loads the contents of a php into a modulewrapper is not a good idea.

So It seems that HTML5 History API is the best approach to this according to that articale, so my question is, how do I go about implementing this HTML5 History API, or would I be able to implement the HTML5 H.API Along with my Ajax Module wrapper?

<?php  include ("./inc/vis1.php"); ?> // this is my music player
<?php

      if (isset($_SESSION["user_login"])) 
        {
        echo '
            <div id="menu">
                <a href="home">Home</a>
                <a href="'.$use.'">Prof</a>

                <a href="set.php">Set</a>
                <a href="msmyg.php">Me</a>

                <a href="frireq.php.php">Fr</a>
                <a href="nudg.php">Nudg</a>
                <a href="logout.php">Logout</a>

            </div>
            ';
        } 
        else{

            echo'<div id="menu">
            <a href="index.php"/>S</a>
            <a href="index.php">L </a>
            </div>  
        ';
        }
        ?>

    <div id="ihed"><div>
       <script src="./js/jquery-2.1.4.min.js"></script>
       <script src="./js/general.js"></script>
</html>     
<footer>
    <div id="c1">           
        <?php echo "Hedi Bej &copy; i-neo 2015"; ?>
    </div>
</footer>

This is my Javascript Ajax general.js file

(function() {

$('#ihed').load('home.php');
var $moduleWrapper = $('#ihed');
var loadIntoModuleWrapper = function(e) {
    e.preventDefault();
    var $anchor = $(this);
    var page = $anchor.attr('href');
    $moduleWrapper.load(page);
};

var sendFormAndLoadIntoModuleWrapper = function(e) {
    e.preventDefault();
    var $form = $(this);
    var method = $form.attr('method') || 'GET';
    $.ajax({
        type: $form.attr('method') || 'GET',
        url: $form.attr('action'),
        data: $form.serialize(),
        success: function(data) {
            $moduleWrapper.html(data);
        }
    });
};


// manage menu links to load content links onto module wrapper
$('.menu').on('click', 'a', loadIntoModuleWrapper);

// manage module-wrapper links to load onto module wrapper
$moduleWrapper.on('click', '.open', loadIntoModuleWrapper);

// manage submits form and load result onto module wrapper
$moduleWrapper.on('submit', '.open-form', sendFormAndLoadIntoModuleWrapper);
}());

Thanks, How do I go about doing it?

Community
  • 1
  • 1
Hedi
  • 139
  • 10
  • 3
    @JoseManuelAbarcaRodríguez I think you're misunderstanding the difference between websites that play music and a music website service... – Hedi Jun 12 '15 at 20:35
  • 1
    You misunderstood the article linked from the other question. Its not Ajax OR History API...its both. Ajax to navigate without changing the page. History API to change the URL so if the user refreshes they come back to similarly configured page. – developerwjk Jun 12 '15 at 21:08
  • @developerwjk oh that's actually great news for me then! so i use ajax, I just need to know what i have to do to implement history api :) Thanks for clearing that up :) Appreciate it! – Hedi Jun 12 '15 at 21:14
  • 1
    Look at the demo http://html5demos.com/history See how its changing the url but clearly the page content is being changed with ajax (or maybe just javascript) not actually via the address bar...that's what you want. View source on that. (Make sure to do it on the first page, because after clicking a link you get blank source) – developerwjk Jun 12 '15 at 21:17
  • The difference is the urls there are not real, as it says `Note: since these urls aren't real, refreshing the page will land on an invalid url.` so you need to make sure you use real urls if you want a refresh to come back to same place. – developerwjk Jun 12 '15 at 21:20
  • 1
    the "module wrapper" you are using can continue to work as is, you would just use the history api to change the url each time a new piece of content is loaded, and make sure that if you go directly to that url, you get the same page. – Kevin B Jun 12 '15 at 21:22
  • I think im starting to slowly get it, so instead of inputting "fourth" which will cause an error on page refresh because its not a url, so if you change it to "http://html5demos.com/history/fourth" it will work on page refresh? – Hedi Jun 12 '15 at 21:24
  • @KevinB Thank you KevinB. That is EXACTLY what im trying to do and that is EXACTLY what I'm looking for, but I don't know where or how to start to accomplish it! do you have any good direction for a novice like myself? – Hedi Jun 12 '15 at 21:26
  • Nope, sorry. it's not really a simple drop in and implement kind of thing, it has to start with being able to serve your site as a static site, and then on the client intercepting the navigation with javascript and replacing it with ajax requests while handling the history object properly to change the url and keep a proper browser history. Try looking at the jQuery BBQ plugin for an example. – Kevin B Jun 12 '15 at 21:27
  • So would you be able to provide a simple small example perhaps so that I can replicate it, fiddle with it and eventually implement it on my website? – Hedi Jun 12 '15 at 21:29

1 Answers1

1

Sound cloud keeps playing the music because the page doesn't change really. They make a couple of ajax request to load info and show you page info based on that requests.

Then, like you said they may be using HTML5 History API to save page states so users can "go back" or "go foward".

If you refresh page the sound will stop because a document request will be made to the server.

I'm not familiar with this kind of architecture but i think you may accomplish this with ease using angularJS. Give it a try:

https://www.codeschool.com/courses/shaping-up-with-angular-js

EDIT

Here's how you can change your links: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

Example:

var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");

This would show yourwebsite.com/bar.html on browser url.

The stateObj is an object associated to push state and the "page 2" is the page title, but for now this parameter is ignored by browsers.

  • Thank you for attempting to answer my question, Appreacite it, Because i also use ajax request to load info which I've already said. This makes it easier for me to implement html5 history api, problem is - how would I go about doing it? I don't understand the relation with angular.js and html5 history api, could you please clarify that bit at least? – Hedi Jun 12 '15 at 21:17
  • @Hedi angularjs already does a lot of the routing you need, however, i don't think it would be applicable to your case unless you want to do a full re-write of your application. – Kevin B Jun 12 '15 at 21:23
  • I definitely want to avoid that, I think I found what Im looking for, it's what you said "the "module wrapper" you are using can continue to work as is, you would just use the history api to change the url each time a new piece of content is loaded, and make sure that if you go directly to that url, you get the same page." – Hedi Jun 12 '15 at 21:27
  • What i mean was if you want to keep using your code, you just have to make your app load pages using ajax request and then use history api to change url. – Ricardo Neves Jun 13 '15 at 10:31
  • firstly it's not an app, it's website, and i already have said, my website already loads pages using ajax request, i just want to know HOW do i use history api to change the url. Im in the blank with this one. I need an easy guide at least to help me with this. – Hedi Jun 20 '15 at 12:01