大网数据广告活动
网站运营与故障问答

SEO for importing the page header with JavaScript rather than PHP

· 52CE 我爱测

My website has the same header/sidebar/footer on every page, so I have been importing them using PHP. To do this I forced all html files to be processed through the apache servers PHP handler via htaccess. I have come to understa…

问题原文

原帖链接 · Kyle Postlewait · CC BY-SA 3.0

My website has the same header/sidebar/footer on every page, so I have been importing them using PHP. To do this I forced all html files to be processed through the apache servers PHP handler via htaccess. I have come to understand that is not necessarily the best thing to do and so found out how to do everything I need using simple JavaScript. I also store a variable in the initial page that then gets used in the imported html.

Questions: By switching from:

<?php include "_header.html";?>

to

<script type="text/javascript" src="_header.js"></script>

I am noticing that the "view source" shows the header.js link, whereas the PHP method will show the source as if the html had been there all along. Will search engines recognize the html in the JavaScript just as they would when loaded with PHP?

I understand it is bad practice to alter the page contents -after- it is downloaded, but this doesn't fall into that category does it? That would detail things like after a wait function, or after user interaction, correct?

Are there any other foreseeable downfalls to switching to JavaScript and turning off the forced PHP processing?

已采纳解答

原帖链接 · Jeff Taylor · CC BY-SA 3.0

You should use the php method. PHP is processed on the server, so the full page layout is put together before it is sent to the browser. JavaScript is run in the browser once the rest of the page has loaded.

So with your JavaScript method, there is a chance that the page will be shown to the user before the header has been inserted.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-01-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。