gpt4 book ai didi

javascript - 使用javascript更改页面标题

转载 作者:行者123 更新时间:2023-11-30 13:26:02 26 4
gpt4 key购买 nike

function changeTitle(title) 
{
document.title = title;
}

function result()
{
var fetchTitle;
fetchTitle = new ActiveXObject("Microsoft.XMLHTTP");
fetchTitle.open("GET", "title.php", true);
fetchTitle.send(null);
}

setInterval('changeTitle(result())', 1000);//check every second for update

标题.php

<?php echo "This is a new title"; ?>

好像没有改标题。为了显示 title.php,我是否遗漏了什么?

谢谢。

最佳答案

Am I missing something

很多东西

  1. 您的 changeTitle 函数需要传递一个参数(一个字符串),您总是将 result 的返回值传递给它,它始终是 undefined 作为 result 没有 return 语句。
  2. 您正在使用 IE 6/7 ActiveX 方法来实现 Ajax,而不是现代的、标准的、跨浏览器兼容的方法
  3. 您没有分配 onReadyStateChange 事件处理程序,因此您对从 XMLHttpRequest 对象返回的数据不做任何事情
  4. 您正在发出异步请求,但您的方法似乎是基于同步请求,因此基于同步请求也会失败(请注意,同步请求会锁定页面,应避免)。

我建议阅读 an introduction to XMLHttpRequest

关于javascript - 使用javascript更改页面标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8437924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com