Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the best way to force a hashchange?
    primarykey
    data
    text
    <p>My website uses hashchange-triggered AJAX (to make it more bookmark-friendly). The problem I am having is that when I click "submit" in a form, all the form data that is <code>serialize()</code>'d to be sent via <code>$.post()</code> gets lost. I know this because I get the "Flag 1" alert after I click submit, and various other tests (alerting, echoing, etc.) show this to be true.</p> <p>Here's my current code:</p> <pre><code>$(document).ready(function() { var data = ''; var hash = ''; newPage(); alert('Flag 1'); $(window).bind('hashchange', function() { hash = window.location.hash; if (hash == '') { path = window.location.pathname; hash = '#' + path.replace(/^\/+/, ''); } data += '&amp;func=' + hash; var xhr = $.post(hash, data, function(result) { $("maincontent").html(result); }) .done(newPage); }); // Initialize vars and handle new form elements function newPage() { data = ''; $('form').submit(function() { data = $(this).serialize(); // Flag 2 - What do I do here? }); } // Load ajax content on first run of document if ($('#maincontent').html() == '') $(window).trigger('hashchange'); }); </code></pre> <p>What I am trying to do is <em>manually</em> fire a hashchange event while also changing the URL. The trouble is that if I just set <code>window.location.hash = $(this).attr('action');</code> then <code>return false;</code> where the "Flag 2" comment is, then I wind up getting unwanted trash in the URL, possibly due to the hashmark being encoded for a URL (...%23, etc).</p> <p>I am wondering what the best way to set the hash is, and whether there is a simpler way to do what I am trying to do to begin with.</p> <p>(I'm also open to comments suggesting alternate approaches for the style of navigation I am trying to achieve)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload