Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails ajax URL change

I have the next issue, I have page which opens with ajax and change dynamicly URL of browser like this

window.history.pushState(null, null, "/desktop/manage/add");

so when I am in the "add" page and open any page with get/post request(even another web site) and push "Go back" button in browser I get not the full html code of page but only ajax part. Like this:pic

How this can be fixed?

P.S If I delete window.history.pushState(null, null, "/desktop/manage/add"); everything works fine, but I need to change URL of browser.

UPD: I open this page via post request, NO ajax enter image description here

THen I open "manage/add" page via AJAX enter image description here

Then go to google.com(any site) and Click back button enter image description here

And my ajax loaded page is not full HTML page, There is only ajax part only like text: enter image description here

I read about this issue, and ussually rails programmers add event to back click button to application.js like this:

$(window).on('popstate', function () {
    $.get(document.location.href)
  });

But this works only is user go not to google(or any other site), it works only if they go through my site and click back button.

UPD2: /add controller

def addProduct

@categories = Market.where("depth = ? and title != ''", 0).reorder(:title);
        @tags = UserTag.where(user_id: current_user.id).includes(:tags);
        respond_to do |format|
            format.html{render layout: "desktop_layout"}
            format.js {}
        end
    end

here is my addProduct.js: (this rendered file you see in screenshot after back button clicked)

window.history.pushState('page212', 'Mikee.kz', '/desktop/manage/add');
    $("#rightBlock").html("<%= escape_javascript(ajax_section id:'page', :render => 'myadd') %>");
like image 291
mondayguy Avatar asked Jul 21 '15 10:07

mondayguy


1 Answers

I had this same problem a while back, however with replaceState() instead. I didn't notice this at first, but my fellow developer called it to my attention. It turned out that I didn't notice it because I was using Firefox and this seems to be a bug exclusive to Chrome/Chromium, which he was using. In fact, it seems to have been a bug for a while now, but Google hasn't fixed it yet.

That being said, it seems that there is no real fix for this, at least until Google fixes it. Of course, simply reloading the page will make the problem go away for the moment, but you still see the ugly code. And you could just not use Chrome/Chromium, but that really isn't a solution either.

like image 169
Ryan K Avatar answered Oct 04 '22 05:10

Ryan K