Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果没有JavaScript中的document.write(),如何创建动态版权日期?_Javascript_Date_Document.write_Copyright Display - Fatal编程技术网

如果没有JavaScript中的document.write(),如何创建动态版权日期?

如果没有JavaScript中的document.write(),如何创建动态版权日期?,javascript,date,document.write,copyright-display,Javascript,Date,Document.write,Copyright Display,如果不使用JavaScript中的document.write(),如何获取和打印当前年份 这是一个典型的版权行在页脚;e、 g.©2017堆栈溢出 现在我正在使用标准的document.write()方法 document.write(new Date().getFullYear()); document.getElementById(“当前年份”).innerHTML=新日期().getFullYear() 是确保可以使用元素: <p>© Stack Overflow <

如果不使用JavaScript中的
document.write()
,如何获取和打印当前年份

这是一个典型的版权行在页脚;e、 g.©2017堆栈溢出

现在我正在使用标准的
document.write()
方法

document.write(new Date().getFullYear());
document.getElementById(“当前年份”).innerHTML=新日期().getFullYear()

是确保可以使用元素:

<p>© Stack Overflow <span id="year"> </span></p>
或者,您可以简单地使用php,如下所示:

<?php echo date("Y"); ?>


如果您使用的是php,请确保将文件ext更改为php。

上面由user3731460发布并由user663031编辑的非常简单的解决方案工作得非常好

在这一页的帖子上方,一位评论者问“使用document.write有什么问题(除了取决于客户机设置为正确的年份之外)?”

我不知道2017年的原因(当这个问题最初被问到这里时),但现在(2020年10月),chrome的lighthouse extension发出警告:“避免document.write,因为它可以延迟页面加载几十秒。”谷歌鼓励用户体验的方式,我可以看出谷歌可能会在某个时候惩罚document.write的使用。

使用JavaScript:

Copyright &copy <script type="text/javascript"> document.write(new Date().getFullYear());</script> [companyName] Communications Inc. All rights reserved.
这是一个关于PHP的例子:

&copy; <?php
  $fromYear = 2008;
  $thisYear = (int)date('Y');
  echo $fromYear . (($fromYear != $thisYear) ? '-' . $thisYear : '');?> CompanyName.
©;公司名称。

document.getElementById(“someElementId”).innerHTML=new Date().getFullYear()?(这个的日期/版权部分是不相关的,对吗?就像在中一样,你只需要能够显示来自JS的任何值?)这让我走上了正确的道路。谢谢使用document.write有什么问题(除了取决于要设置为正确年份的客户端之外)?@RobG返回错误:
[Violation]避免使用document.write()。
,因为它会对性能产生不利影响。非常简单和接近,喜欢它!评论的目的是让OP思考为什么document.write在这种情况下可能是好的或坏的。不使用它,因为“谷歌开发工具说不要使用它”不是一个合乎逻辑的论点。
Copyright &copy <script type="text/javascript"> document.write(new Date().getFullYear());</script> [companyName] Communications Inc. All rights reserved.
<div id="footerLinks" style="clear:left; text-align:left; color:#282828">
    Copyright &copy <script type="text/javascript"> document.write(new Date().getFullYear());</script> DigiVie Communications Inc. All rights reserved.
  </div>
&copy; <?php
  $fromYear = 2008;
  $thisYear = (int)date('Y');
  echo $fromYear . (($fromYear != $thisYear) ? '-' . $thisYear : '');?> CompanyName.