如何获取标题的内部html
how to get the inner html of the title
我试图 link 我的页面到另一个页面而不刷新页面,我成功了,但是第二页的标题没有改变它仍然是第一页的标题。第二页有自己的包含页面标题的标题标签,但是当我重定向它时它没有反映,我该如何解决。
这是我的代码:
let script = document.createElement("script");
script.src = "https://code.jquery.com/jquery-3.4.1.min.js";
script.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(script);
function goto(name_of_page) {
// go get the page and paste it on the current page
var request = new XMLHttpRequest();
request.open("GET", name_of_page, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
let resp = request.responseText;
let link = name_of_page.split(".");
let name = link[0];
$("body").load(`${name_of_page}`);
if (
location.href.includes("localhost") ||
location.href.includes("www")
) {
window.history.pushState(name_of_page, "Title", name);
console.log("has www or localhost");
} else {
window.history.pushState(name_of_page, "Title", name_of_page);
console.log("does not have www");
}
}
};
request.send();
}
您可以将加载的数据解析为 HTML Content Template,因此一旦您拥有数据:
/*...*/
let resp = request.responseText;
const tpl = document.createElement('template');
tpl.innerHTML = resp;
document.title = tpl.content.querySelector('title').textContent;
/*...*/
我试图 link 我的页面到另一个页面而不刷新页面,我成功了,但是第二页的标题没有改变它仍然是第一页的标题。第二页有自己的包含页面标题的标题标签,但是当我重定向它时它没有反映,我该如何解决。
这是我的代码:
let script = document.createElement("script");
script.src = "https://code.jquery.com/jquery-3.4.1.min.js";
script.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(script);
function goto(name_of_page) {
// go get the page and paste it on the current page
var request = new XMLHttpRequest();
request.open("GET", name_of_page, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
let resp = request.responseText;
let link = name_of_page.split(".");
let name = link[0];
$("body").load(`${name_of_page}`);
if (
location.href.includes("localhost") ||
location.href.includes("www")
) {
window.history.pushState(name_of_page, "Title", name);
console.log("has www or localhost");
} else {
window.history.pushState(name_of_page, "Title", name_of_page);
console.log("does not have www");
}
}
};
request.send();
}
您可以将加载的数据解析为 HTML Content Template,因此一旦您拥有数据:
/*...*/
let resp = request.responseText;
const tpl = document.createElement('template');
tpl.innerHTML = resp;
document.title = tpl.content.querySelector('title').textContent;
/*...*/