JavaScript What's the best way to force a hashchange? whataburger,whatsapp,what time is it,what is a bear

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 serialize()'d to be sent via $.post() 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.

Here's my current 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 += '&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');

});

What I am trying to do is manually fire a hashchange event while also changing the URL. The trouble is that if I just set window.location.hash = $(this).attr('action'); then return false; 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).

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.

(I'm also open to comments suggesting alternate approaches for the style of navigation I am trying to achieve)

Answer:1

In the example below, map is called twice per onNext call which is unnecessary, because the same value for ds can be reused for the two observers. How to write the code such that the map is only ...

In the example below, map is called twice per onNext call which is unnecessary, because the same value for ds can be reused for the two observers. How to write the code such that the map is only ...

I am not able to set maximum height on the slider I am using, every height I set doesn't seem to be working. I have also tried setting the width on echo img row on the php part but it is not working. ...

I am not able to set maximum height on the slider I am using, every height I set doesn't seem to be working. I have also tried setting the width on echo img row on the php part but it is not working. ...

  1. setting height 100 not working
  2. setting div height not working
  3. jquery setting height not working

I have a lot of problems in my code because it is not synchronous. Here is an example of problem that i have in a chrome extension. This is my function function getTranslation(a_data, callback) { ...

I have a lot of problems in my code because it is not synchronous. Here is an example of problem that i have in a chrome extension. This is my function function getTranslation(a_data, callback) { ...

  1. javascript synchronous functions
  2. js synchronous functions
  3. javascript synchronous callback
  4. javascript synchronous calls
  5. javascript synchronous method
  6. javascript synchronous method call
  7. js synchronous callback
  8. js synchronous method
  9. javascript callback synchronous or asynchronous
  10. node js synchronous functions
  11. javascript synchronous and asynchronous functions
  12. javascript make functions synchronous
  13. javascript synchronous vs asynchronous functions

I want to reload a template and the connected Controller without using $routeProvider, because my Path-structure doesn't fit with .when(...). I want to use $location.path() to read the URL and set the ...

I want to reload a template and the connected Controller without using $routeProvider, because my Path-structure doesn't fit with .when(...). I want to use $location.path() to read the URL and set the ...

  1. angular reload template
  2. angular 6 reload template
  3. angular directive reload template
  4. angular 2 reload template
  5. angular force reload template
  6. angular 2 reload component template
  7. angular 2 force template reload
  8. angularjs reload template