66953

Javascript/Jquery : Back button not changing content at all

Question:

<script> $(function(){ $("a[rel='tab']").click(function(e){ e.preventDefault(); pageurl = $(this).attr('href'); $.ajax({url:pageurl+'&rel=tab',success: function(data){ $('#right_column').html(data); }}); if(pageurl!=window.location){ window.history.pushState({path:pageurl},'',pageurl); } return false; }); }); /* the below code is to override back button to get the ajax content without reload*/ $(window).bind('popstate', function() { $.ajax({url:location.pathname+'&rel=tab',success: function(data){ $('#right_column').html(data); }}); }); </script>

I pulled this code off a demo and am modifying it to fit my particular project; however, am attempting to run it as it is to test out features. The demo worked perfectly. The only major difference is they are using jquery 1.4.4 and I am using jquery 1.9.1. I cannot seem to get the back button to work correctly. The url changes when hitting back; however, the #right_column doesn't update at all. I copied this code directly off a demo and adjusted the div id to match mine, and it still doesn't work. The below line of code is the questionable code.

/* the below code is to override back button to get the ajax content without reload*/ $(window).bind('popstate', function() { $.ajax({url:location.pathname+'&rel=tab',success: function(data){ $('#right_column').html(data); }}); });

Also, can I use location.pathname.replace('index.php', 'view.php') ? Not sure if this is the correct way of writing that particular code to replace index.php?variables... with view.php?variables... to load that page into the right column. See my other post if this part of the question confuses you... <a href="https://stackoverflow.com/questions/20754212/javascript-jquery-need-to-substring-in-jquery" rel="nofollow">javascript/jquery: Need to substring in jquery</a> (modified code)

Answer1:

For those that this may help, this ended up fixing my code and it now responds to back button.

$(window).on('popstate', function() { $.ajax({url:$(location).attr('href').replace('index.php', 'rightcolumn.php') +'&rel=tab',success: function(data){ $('#right_column').html(data); }}); });

Recommend

  • Render Backbone View on click (via Router push state)
  • $stateChangeStart run before getting data from Rest API
  • typescript history js conflict
  • Why `Failed to execute 'pushState' on 'History'` when using `Link` from react-ro
  • django server code not updating
  • Problem with Django using Apache2 (mod_wsgi), Occassionally is “unable to import from module” for no
  • Knockout custom binding handler
  • Position: fixed nav does not stay fixed
  • Android fill_parent issue
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • jQuery show() function is not executed in Safari if submit handler returns true
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • TFS: Get latest causes slow project reloading
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Acquiring multiple attributes from .xml file in c#
  • Free memory of cv::Mat loaded using FileStorage API
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to get Windows thread pool to call class member function?
  • How can I remove ASP.NET Designer.cs files?
  • Bitwise OR returns boolean when one of operands is nil
  • unknown Exception android
  • sending mail using smtp is too slow
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?