最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - ok to include css and js files outside <html>? - Stack Overflow

programmeradmin0浏览0评论

i wonder if i could embed js and css files above the html document scope:

<script type="text/javascript" src="../../media/js/jquery.js"></script>
<script type="text/javascript" src="../../media/js/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="../../media/css/cupertino/jquery-ui.css" />

<html>
   <head>
   </head>
   <body>
   <body>
</html>

this is because i want to put them in same php file where i include all files (php, js and css). i have tried this and it seems to work. the output of the html file will be shown just like the above code, with the include rows above the html tag.

are there any hidden cons in this?

i wonder if i could embed js and css files above the html document scope:

<script type="text/javascript" src="../../media/js/jquery.js"></script>
<script type="text/javascript" src="../../media/js/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="../../media/css/cupertino/jquery-ui.css" />

<html>
   <head>
   </head>
   <body>
   <body>
</html>

this is because i want to put them in same php file where i include all files (php, js and css). i have tried this and it seems to work. the output of the html file will be shown just like the above code, with the include rows above the html tag.

are there any hidden cons in this?

Share Improve this question asked Feb 1, 2010 at 1:12 ajsieajsie 79.7k110 gold badges284 silver badges386 bronze badges 2
  • 2 Could you elaborate on the reason you need to do this? How putting them in the same php file forces you to put them at the top? – Majid Fouladpour Commented Feb 1, 2010 at 1:25
  • Yeah, there's absolutely no reason why you couldn't do your includes after outputting <html><head>. – bobince Commented Feb 1, 2010 at 15:50
Add a comment  | 

6 Answers 6

Reset to default 10

Even if it works, you shouldn't do it. This type of stuff is sloppy, and as such isn't guaranteed to work tomorrow, or in future browsers. If you don't feel the agony of this method now, you will eventually. There's no reason that you should be doing this anyway.

This isn't valid html. The best place to put the javascript would be before the body close (unless there's in-line scripts that need those scripts to be loaded). This prevents blocking as the page loads.

Will not be valid (X)HTML.

This will work in most all browsers, but that's not to say it isn't wrong. It is wrong.

It's not valid HTML, and will confuse just about everyone who comes across your code, and though I don't know what browsers could possibly fail to overcome the inherent wrongness about this style, I make no promises that it will work. In a sense, it should, but in another, it most definitely should not.

Perhaps output buffering will work in this situation? Buffer the output from your "includes" file, then grab the contents of the buffer to output later, after the <html> declaration. Something roughly like this:

In your includes.php file:

<?php 
ob_start();
// here is where you output your css and js declarations
$includes = ob_get_clean();
?>

And here is your main page:

<html>
 <head>
  <title>Hello</title>
  <?php echo $includes ?>
 </head>
 <body>
 ...

I know this is very old now, but I want to add that Google is recommending to do this in certain cases.

Take a look at this: https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery#example

Any thoughts as to why Google is advocating improper HTML coding?

发布评论

评论列表(0)

  1. 暂无评论