views:

232

answers:

5

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?

+5  A: 

Will not be valid (X)HTML.

Alan Haggai Alavi
but how come that it works on firefox?
weng
Working, and validating are different. Google's pages work in all browsers, however, they do not validate successfully.
Alan Haggai Alavi
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.
Alex JL
I've seen title tags at the bottom of a page. Browsers are extremely forgiving. Maybe too forgiving.
John Conde
+6  A: 

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.

Nick Craver
It's not valid HTML anyway, there's no DTD.
bdl
+2  A: 

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.

Matchu
+9  A: 

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.

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

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