0

In my project I am using this Ajax function (below) to get the content (it works really well ^^).

Anyway I would like to have a nicer looking URL (to be honest I am no fan of the hashtag,) with the script I have been using until now I get something like this: http://example.com/#sitename

To change that I did some research: I learnd this: the window.history.pushState('page2', 'Title', '/page2.php'); JavaScript line (see here or here more information on updating URL without reloading)

I should be able to actually change the url so that I would like it. now my problem is that I am a total beginner whit programming (And when I tried to include it in to the code it broke and nothing worked anymore.) and wanted to ask if someone could help me include it in the code (below)

Also I heard of the History.js - library if I read correctly this should also should do the trick whit, but like I said I am inexperienced and have to ask how to include it to the ajax-call (I mean to include it in to my code) or is that not possible?

 $(document).ready(function() {
        var e = "", t = $("#content");
        $("ul:not(.no) li a").click(function() {
            window.location.hash = $(this).attr("href");
            return false;
        });
        $(window).bind("hashchange", function() {
            e = window.location.hash.substring(1);
            url = e;
            if (e) {
                t.fadeOut(200, function() {
                    url = url.replace("#", "");
                    $("#loading").css("visibility", "visible");
                    $.ajax({
                        type : "POST",
                        url : "load_page.php",
                        data : "page=" + url,
                        success : function(e) {
                            if (parseInt(e) != 0) {
                                t.fadeIn(200, function() {
                                    $("#content").html(e);
                                    var $title = $("#title").text();
                                    document.title = "JuBla | " + $title;
                                });
                            }
                            $("#loading").css("visibility", "hidden");
                        }
                    });
                    $("li a").removeClass("current");
                    $('li a[href="" + e + ""]').addClass("current");
           //my attempted solution                      //    window.history.pushState('/'+ url, $title, '/' + url + '/')
                });
            }
        });
        $(window).trigger("hashchange");
    });
Community
  • 1
  • 1
adho12
  • 395
  • 4
  • 11
  • You should include the attempted solution – Oriol Nov 01 '13 at 21:00
  • @nietonfir it is special for this code – adho12 Nov 01 '13 at 21:12
  • @Oriol see the Commented part – adho12 Nov 01 '13 at 21:12
  • @adho12 I don't understand. You say you want to get rid of hashes, and use new history API instead; but you try to use the new API inside a `hashchange` event listener. If you don't want hashes, why are you trying to use `hashchange`? – Oriol Nov 01 '13 at 22:23
  • @Orion That is my main problem I don't realy get this code so i asked how to rewrite it so that I coud define the URL my self – adho12 Nov 02 '13 at 15:03

0 Answers0