0

In the below posts, I'm adding string #show-post to the current URL .I will have a simple function to trigger a pop up box when the string present in the URL. This is an alternative idea as $(".posts-popup").trigger('click'); is not working oddly, its placed in the success function of ajax.

ANd this adds the string multiple time like this:

http://localhost/homepage.php#show-post#show-post#show-post#show-post#show-post#show-post#show-post#show-post#show-post#show-post#

ANd problem with adding the string to URL, each time the page is reloaded and the string is present, this pop up box function triggered. SO I'm looking for a way to remove the string from URL when other ajax calls are made.

window.location.href.split(/[?#]/)[0];//to remove the string.

I referred here: Remove querystring from URL I thought this would, immediately remove the string when I place this in the ajax success function, but it doesn't remove at all.How to use this then?

//string added in the success function of ajax

$("input[name^=delete2]").on("click",function()
    {

    var deleteMe = this.id;

    $.ajax({
    dataType: "text",
    url: '/delete_this.php?id='+deleteMe,
    success: function(data){

      //$(".posts-popup").trigger('click');
     window.location = window.location.href + "#show-post";
     window.location.reload();

      }//end sucess
    });//end ajax

  });
Community
  • 1
  • 1
112233
  • 2,406
  • 3
  • 38
  • 88
  • When appending a new hash in URL, first check if URL already contains it. If not then only add it. – Tushar Oct 31 '15 at 14:06
  • just set `window.location.hash`. Not totally clear what you are trying to do – charlietfl Oct 31 '15 at 14:10
  • @Tushar, I checked like this: success: function(data){ if (window.location.href.indexOf("#show-post") != -1) { window.location = window.location.href + "#show-post"; window.location.reload(); } }//end sucess – 112233 Oct 31 '15 at 14:11
  • @Tushar, but that prevent that main ajax function from happening and doesn't add string – 112233 Oct 31 '15 at 14:12
  • @charlietfl Is right here to use `window.location.hash`, set the value to it. – Tushar Oct 31 '15 at 14:12
  • @charlietfl, thanks, I've successfully added the string via hash..but how to remove the string from URL in other ajax success function? – 112233 Oct 31 '15 at 14:23

1 Answers1

1

You could try this:

var state = { yourState: foo },                        
var newURL = window.location.pathname+"#show-post";
window.history.pushState( state , null, newURL); 

Cheers

S. F.
  • 206
  • 4
  • 14
  • what whould be there in the var state ? – 112233 Oct 31 '15 at 14:35
  • Check this previous answer on stackoverflow: http://stackoverflow.com/questions/17612307/pushstate-what-exactly-is-the-state-object-for. Btw, I am not sure if I got your question correctly. If you want to remove the "#show-post" then use var pathArray = window.location.pathname.split( '#' ); pathArray[0]; – S. F. Oct 31 '15 at 14:40
  • should note that older browsers don't support `history.pushstate` – charlietfl Oct 31 '15 at 15:06
  • oh i see, ill find some alternative for that – 112233 Oct 31 '15 at 15:14
  • yes old browsers won't support it. But newer browsers, which use the html5 engine are supporting it: https://en.wikipedia.org/wiki/Comparison_of_layout_engines_%28HTML5%29 – S. F. Nov 01 '15 at 12:21