Code前端首页关于Code前端联系我们

如何在js中更改页面URL而不刷新页面

terry 2年前 (2023-09-09) 阅读数 215 #Javascript

适用于Chrome,Safari,Firefox 4+和Internet Explorer 10或更高版本

文字

实现代码如下:

/**
 * 时间:2019年8月18日
 * 家庭教程:https://www.codeqd.com/wp-content/uploads/2023/09/
 */
函数 processAjaxData(响应,urlPath){
    document.getElementById("内容").innerHTML = response.html;
    document.title = 响应.pageTitle;
    window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
}

//可以使用window.onpopstate检测浏览器前进后退按钮
window.onpopstate = 函数{
    if(e.state){
        document.getElementById("内容").innerHTML = e.state.html;
        document.title = e.state.pageTitle;
    }
};

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门