jQuery函数从URL中删除其他参数

问题描述 投票:0回答:1

我有以下jQuery代码,在单击后退按钮时会关闭引导程序模式。一切正常。但是,当关闭模式时,应该从URL栏中删除#my-modal-id。但是,与此同时,它也会从URL中删除其他“必需”参数。例如:

URL为http://localhost/gamearena/index.php#my-modal-id时,效果很好。在此,它按照脚本从URL栏中删除#my-modal-id。到目前为止,一切都很好。

但是,当URL具有其他参数时,例如http://localhost/gamearena/index.php?ref=shreyansh#sidebar-left。在这里,它甚至会删除?ref=shreyansh。我如何控制它,以便它仅删除之前由其自身设置的模式ID。

这里是代码:

// Close model on clicking back button / swiping back
  $('div.modal').on('show.bs.modal', function() {
    var modal = this;
    var hash = modal.id;
    window.location.hash = hash;
    window.onhashchange = function() {
      if (!location.hash){
        $(modal).modal('hide');
      }
    }
  });
  $('div.modal').on('hidden.bs.modal', function() {
    var hash = this.id;
    history.replaceState('', document.title, window.location.pathname);
  });
  // when close button clicked simulate back
  $('div.modal button.close').on('click', function(){
    window.history.back();
  })
  // when esc pressed when modal open simulate back
  $('div.modal').keyup(function(e) {
    if (e.keyCode == 27){
      window.history.back();
    }
  });
javascript html jquery twitter-bootstrap bootstrap-modal
1个回答
0
投票

尝试

window.location.hash = '';

代替使用

window.history.back();

如果要从URL中删除哈希。它还将触发window.onhashchange

© www.soinside.com 2019 - 2024. All rights reserved.