3

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?

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? Commented Feb 1, 2010 at 1:25
  • Yeah, there's absolutely no reason why you couldn't do your includes after outputting <html><head>. Commented Feb 1, 2010 at 15:50

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

true.but then again, many frameworks use this technique and keep them in a separate php file.
@pixeltocode: You can do that and still load them up in the proper place.
6

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.

1 Comment

It's not valid HTML anyway, there's no DTD.
5

Will not be valid (X)HTML.

3 Comments

Working, and validating are different. Google's pages work in all browsers, however, they do not validate successfully.
Browsers are made to do the best they can to cope with broken HTML, so Firefox is doing what it can. It probably puts in an implicit <html> at the start and discards the next one.
I've seen title tags at the bottom of a page. Browsers are extremely forgiving. Maybe too forgiving.
2

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.

Comments

1

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>
 ...

Comments

0

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?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.