动态HTML标题元素(Dynamic HTML title element)

编程入门 行业动态 更新时间:2024-10-17 00:27:29
动态HTML标题元素(Dynamic HTML title element)

我试图从PHP动态设置页面的HTML标题。 我有一个页面,它根据数据库中的条目设置title元素。 我试图根据当前页面的H2内容动态更改标题。 再次从数据库中检索此内容。

我试图使用会话变量来做到这一点,但显然由于加载顺序,这在加载标题时不起作用,然后是内容。 在页面刷新,然后正确设置标题,但这不是很好。

我目前正在使用JavaScript来更新标题,但这对于没有启用JS的搜索引擎机器人来说也没有用。

PHP

session_start(); <--both header and dynamic page --> <title><?php echo $_SESSION['dynamictitle'];?></title> <-- Header --> $jobTitle = $rs2row['fldRoleTitle']; <-- dynamic page --> $_SESSION['dynamictitle'] = $jobTitle;

JavaScript的

var currentTitle = "<?php Print($jobTitle) ?>" + " | " + document.title; document.title = currentTitle;

I am trying to set the HTML title of a page dynamically from PHP. I have a page which sets the title element based upon an entry in a database. I am trying to make the title change dynamically based on the current page's H2 content. This content is, once again, retrieved from the database.

I have tried to do this using session variables, but obviously due to the load order this doesn't work as the header is loaded, and then the content. On page refresh the title is then set correctly, but this is not good.

I currently am using JavaScript to update the title but again this is no good for search engine bot that don't have JS enabled.

PHP

session_start(); <--both header and dynamic page --> <title><?php echo $_SESSION['dynamictitle'];?></title> <-- Header --> $jobTitle = $rs2row['fldRoleTitle']; <-- dynamic page --> $_SESSION['dynamictitle'] = $jobTitle;

JavaScript

var currentTitle = "<?php Print($jobTitle) ?>" + " | " + document.title; document.title = currentTitle;

最满意答案

将模板的数据加载和处理与模板的实际输出/呈现分开,例如在将变量放入模板之前确定变量,例如

<?php // handlerForThisPage.php session_start(); $dynamicTitle = $_SESSION['dynamictitle']; … $jobTitle = $rs2row['fldRoleTitle']; … include '/path/to/header.html'; include '/path/to/templateForThisPage.html';

然后只需回显相应模板中的变量,例如

// header.html <html> <head> <title><?php echo $dynamicTitle ?></title> …

什么应该进入templateForThisPage.html然后。 这比在一个大杂乱的文件中混合数据获取,处理和输出的线性脚本更方便和更容易维护。 如果您想扩展此方法,请考虑阅读有关MVC模式的内容。

Separate the loading and processing of the data for the template from the actual output/rendering of the template, e.g. determine the variables before putting them in the template, e.g.

<?php // handlerForThisPage.php session_start(); $dynamicTitle = $_SESSION['dynamictitle']; … $jobTitle = $rs2row['fldRoleTitle']; … include '/path/to/header.html'; include '/path/to/templateForThisPage.html';

And then just echo the variables in the respective templates, e.g.

// header.html <html> <head> <title><?php echo $dynamicTitle ?></title> …

and whatever should go into templateForThisPage.html then. This is much more convenient and sane to maintain than having a linear script mixing data fetching, processing and output in one big messy file. If you want to extend on this approach, consider reading about the MVC pattern.

更多推荐

title,标题,page,JavaScript,电脑培训,计算机培训,IT培训"/> <meta name="desc

本文发布于:2023-07-18 04:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   标题   动态   HTML   element

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!