js之无感知修改请求链接:
$(document).on('mouseenter', "#subscribe_user_comment", function () {
let userId = $(this).data("user");
if ('' === userId) {
let searchParams = window.location.search;
searchParams = searchParams.split('?')[0];
searchParams = updateSearchParams(searchParams, 'subscribe_comment', true);
window.history.pushState('', '', searchParams);
}
});
//window.history.pushState('', '', searchParams); 是 JavaScript 中用于修改浏览器历史记录的方法之一。这个方法是 HTML5 中引入的,它允许你在不刷新页面的情况下改变浏览器的 URL 和历史记录状态。
function subscribeComment(){
if (-1 !== window.location.search.indexOf("subscribe_comment")){
let ele = $("#subscribe_user_comment");
$('html, body').animate({
scrollTop: ele.offset().top
}, 500);
ele.trigger("click");
removeSubscribeCommentParam()
}
}
function removeSubscribeCommentParam() {
let currentUrl = window.location.href;
let updatedUrl = currentUrl.replace(/([?&])subscribe_comment=[^&]*(&|$)/, function(match, p1, p2) {
return p1 === '?' ? p2 : '';
});
if (updatedUrl !== currentUrl) {
window.history.replaceState({}, document.title, updatedUrl);
}
}
//window.history.replaceState 的作用是将当前浏览器历史记录条目的状态修改为提供的状态,并将当前页面的 URL 修改为新的 URL,但并不会导致页面重新加载。这对于在不刷新整个页面的情况下更新 URL 很有用。
window.history.pushState和window.history.replaceState有相似的作用,就上面的需求而言,可以达到相同的效果.
