我有一个动态网站,只有在用户点击页面时才会加载正文。我正在尝试更改标题标签,但我没有运气。
HTML:
<head>
// Title tag is contained in the php file
<?php include (BASEPATH . "includes/widgets/pageTitle.php"); ?>
</head>
的JavaScript / jQuery的:
$(document).on('click', 'a', function(e) {
// Page url of destination
var pageurl = $(this).attr('href');
var baseurl = "http://localhost/offstreams/";
// prevent page from loading
e.preventDefault();
// Empty info inside the body class and reload new info
// THIS WORKS PERFECTLY
$('.body').empty().load(pageurl + " > .body > *");
//!!!!!!!!!!!!!!!!!!!!!
// THIS IS THE PROBLEM
//!!!!!!!!!!!!!!!!!!!!!
$('title').empty().load(pageurl + "> title > *");
// Push the URL state
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
//stop refreshing to the page given in
return false;
}
});
PHP代码片段:
//Band page title tag
if (isset($_GET['member']) && isset($_GET['edit']) && isset($_GET['band'])){
$band_id = $_GET['band'];
$sql = "SELECT `band_name` FROM `bands` WHERE `band_id` = '$band_id'";
$query = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($query)){
$band_name = $row['band_name'];
echo "<title>" . $band_name . " | " . "Offstreams</title>";
}
实际负载的示例输出将是 Count to Four |离线,这就是我想要的。
当我执行ajax加载时,网站可以运行,但标题标记提供默认网址,例如 localhost / offstreams / etc ... ,标题标记变为
<title>
<title>Count to Four | Offstreams</title>
</title>
有谁知道为什么?
答案 0 :(得分:0)
看起来你在那里加倍了标题标签,$('title')。empty()位将离开之前的那些。
尝试将标题标记放在最初的html中:
<head>
// Title tag is contained in the php file
<title><?php include (BASEPATH . "includes/widgets/pageTitle.php"); ?></title>
</head>
将它们从你的php中删除:
echo $band_name . " | " . "Offstreams";
答案 1 :(得分:0)
我不理解在循环中输出title
的原因,因为每页只有一个,除非我在代码中遗漏了某些内容。好像它需要在外面。
if (isset($_GET['member']) && isset($_GET['edit']) && isset($_GET['band'])){
$band_id = $_GET['band'];
$sql = "SELECT `band_name` FROM `bands` WHERE `band_id` = '$band_id'";
$query = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($query)){
$band_name = $row['band_name'];
}
echo "<title>" . $band_name . " | " . "Offstreams</title>";
}
关于您的JQuery脚本,请记住.load() documentation:
jQuery使用浏览器的.innerHTML属性来解析检索到的 记录并将其插入当前文档。在此过程中, 浏览器通常会过滤文档中的元素,例如
<html>
,<title>
或<head>
个元素。结果,检索到的元素.load()
可能与检索文档时不完全相同 直接通过浏览器。
换句话说,您所做的事情可能无法在所有浏览器中正常运行。考虑到这一点,试一试。
$(document).on('click', 'a', function(e) {
// Page url of destination
var pageurl = $(this).attr('href');
// prevent page from loading
e.preventDefault();
// Empty info inside the body class and reload new info
// THIS WORKS PERFECTLY
$('.body').empty().load(pageurl + " > .body > *");
// Give this a try
$(pageurl).load(pageurl, function() {
$('title').load('title', function() {
document.title = $(this).text();
});
});
// Push the URL state
if(pageurl !== window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
//stop refreshing to the page given in
return false;
});