简体   繁体   English

如何获得标题的内部html

[英]how to get the inner html of the title

i was trying to link my page to another page without refreshing the page and i did it successfully, but the title of the 2nd page did not change it was still the title of the first page.我试图在不刷新页面的情况下将我的页面链接到另一个页面并且我成功地做到了,但是第二页的标题没有改变它仍然是第一页的标题。 the 2nd page has its own title tag that contains the title of the page, but it is not reflecting when i redirect it, how can i fix that.第二页有自己的标题标签,其中包含页面的标题,但是当我重定向它时它没有反映,我该如何解决这个问题。

here is my code:这是我的代码:

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();

}

You can parse your loaded data to an HTML Content Template , so once you have the data:您可以将加载的数据解析为HTML 内容模板,因此一旦获得数据:

/*...*/

let resp = request.responseText;

const tpl = document.createElement('template');
tpl.innerHTML = resp;

document.title = tpl.content.querySelector('title').textContent;

/*...*/

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

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