3

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?

6
  • 2
    " I have come to understand that is not necessarily the best thing to do" - why?
    – Steve
    Jan 6, 2017 at 6:18
  • 3
    @Rob Using client side rendering vs server side rendering often has an SEO effect. These days Googlebot is good enough at crawling JavaScript that it may not matter. It would be good to get an answer that addresses that. Jan 6, 2017 at 16:26
  • The PHP include is executed server side and all the code is sent to the browser. This is why you see it. JS is executed within he browser and why the browser show source shows only the link. That is the code it received. Of the two, the PHP is more reliable. Keep in mind that not all users will allow their browser to execute JS. As well, JS code can be inspected prior to executing and can be blocked by some filter or firewall. It is that simple. Cheers!!
    – closetnoc
    Jan 7, 2017 at 1:42
  • Users were suggesting forced PHP recognition of html was a slight security risk and that it was bad practice... I'm not a billion dollar firm and I don't really care about either, so, if the JS is going to affect my SEO, I'm sticking with PHP. thanks for the insight. Server side versus client side is a good way to summarize what I got from this. Jan 7, 2017 at 9:05
  • 1
    I would be interested to see a proposed snippet of your _header.html and _header.js files. I would think that that alone would be enough to dissuade most from using the JavaScript approach. (?)
    – MrWhite
    Jan 7, 2017 at 23:39

1 Answer 1

1

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.

3
  • While I would support your recommendation, your reasoning is wrong. There might be a lot a asynchronous Javascript technologies (you might be thinking about Google Tag Manager which executes tags asynchronously), but per default javascript code will be executed as soon as it is loaded (and will block the rest of the page until it is finished). Jan 6, 2017 at 10:00
  • JavaScript adding the header doesn't seem very different from a user experience standpoint than the browser downloading images and inserting them once they are downloaded. Jan 6, 2017 at 12:01
  • 1
    This isn't maybe the best answer, but it's the only one =o I think it's clear for generaly users like me PHP is a fair way to go about it. Jan 7, 2017 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.