简体   繁体   English

JQuery / JavaScript 在 URL 中添加查询字符串

[英]JQuery / JavaScript Add Query String in URL

I just want to add a Query String in URL when onclick the anchor tag.我只想在单击锚标记时在 URL 中添加一个查询字符串。

URL Like: http://example.me/about/about-me网址喜欢: http : //example.me/about/about-me

Below is my code.下面是我的代码。

$(".popup_dir").on('click', function() { 
    var exmp_id = $(this).attr('exmp_id');
    window.location.search = 'exmp_id='+exmp_id;
});

My Issue is that, when I click it refresh the page and add querystring.我的问题是,当我单击它时刷新页面并添加查询字符串。 But I do not to refresh the page.但我不刷新页面。 I want that onclick event it will add / update the Query String Value.我希望onclick事件将添加/更新查询字符串值。 That's it.就是这样。

After onclick event I want this type of URL.在 onclick 事件之后,我想要这种类型的 URL。

URL Like: http://example.me/about/about-me/?exmp_id=7网址喜欢: http : //example.me/about/about-me/?exmp_id=7

Any Help will be appreciating.任何帮助将不胜感激。

Thanks谢谢

You can use window.history.pushState to add a new item to the browser's history and update the URL bar, without causing the page to reload:您可以使用window.history.pushState向浏览器的历史记录添加新项目并更新 URL 栏,而不会导致页面重新加载:

window.history.pushState({}, "", 'http://example.me/about/about-me/?exmp_id=7');

This function takes three parameters:这个函数需要三个参数:

  • data : Some structured data, such as settings or content, assigned to the history item. data :分配给历史项目的一些结构化数据,例如设置或内容。
  • title : The name of the item in the history drop-down shown by the browser's back and forward buttons. title :浏览器的后退和前进按钮显示的历史记录下拉列表中的项目名称。
  • url : The URL to this state that should be displayed in the address bar (optional). url : 此状态的 URL,应显示在地址栏中(可选)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM