Splitting Tags May Cause A Splitting Head Ache

Sitepoint Members,

If you have the <html> and <head> tags split between an include php file and you have the rest of the <html> and <head> at the top of an html file, can it cause a “specify a character set” error? Are there other problems with doing this? I’m trying to use an include for all that beginning stuff and start the html file with the include reference and then the <title>. Titles are (should be) unique to each html file, but the other code is not - so why not use an include for the non unique code.

Thanks,

Chris

Not sure quite what scenario you are describing, but I’ve never seen a problem with splitting things up in include files.

Raplph,
Thanks for writing. Maybe I should back up. I’m geting a specify a character set error from Pagespeed, yet I have the same DTD code and other inital code in anothe site and no problems. The only difference is the code for one is in wordpress and no error and the code for the other is split up. What should I do to figure out the problem? No errors via Total Validator, nor W3C validation.

Thanks,

Chris

The problem is that the server does not send seem to send the Content-Type header, or at least not with a charset.

Put this in the index.php


header('Content-Type: text/html; charset=utf-8');

:slight_smile:

Scallio,
I put in the header
<meta header(‘Content-Type: text/html; charset=utf-8’) /> and took the same info out of the include. The error I’m getting is

line 3 column 85 - Warning: <meta> attribute “header('content-type:” lacks value.

I tried it without the ’ and ’ and no difference. In case “header” referred to the name of the include file, I changed “header” to the name of the include file and still have the same error - lacks value, specify a charset.

Any ideas?

Thanks,

Chris

It’s not a HTML header, it’s php code :slight_smile:


<?php
header('Content-Type: text/html; charset=utf-8');
?>
<!-- rest of file goes here -->

I’m unsure if

<meta header(‘Content-Type: text/html; charset=utf-8’) />

is meant to be php code or straight HTML. If the latter it’s incorrect

From http://www.w3.org/TR/html4/struct/global.html
“The following example specifies the character encoding for a document as being ISO-8859-5”


<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-5"> 

So simply change that to UTF-8 like so


<META http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

Scallio, Lesley,
<?php
header(‘Content-Type: text/html; charset=utf-8’) ?>
<!-- rest of file goes here –>

Worked (no charset error in PageSpeed), but strangely page source code doesn’t show charset code.

This
<META http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
I hoped would work and tried it prior but I forgot the equiv code. I wold think with the equiv code it would work but it just doesn’t. The quirk in Apache (I guess) made Scallio’s placement comment necessary (<!-- rest of file goes here –>); when I tried putting in the index.html file the include reference first and the <?php header('Content-Type … second, still had the page speed charset error.

Scallio to the rescue once again !!!

Chris

Scallio,
Is that supposed to happen, Content-Type: text/html; charset=utf-8 not showing up in page source code?

Thanks,

Chris

The PHP bit at the top of the page is not meant to appear in the source code. It would cause errors if it did. :slight_smile:

Ralph,

Thanks for the help Ralph. I hardly know anything about php.

Chris

Me either. :smiley: So is the issue resolved? The header that Remon (ScallioXTX) provided should do the trick, but normally the meta tag is included also as a fallback, just after the opening <head> tag.

Ralph,
I put it in and it doesn’t cause any problems, but it seems kinda strange.
The code
<?php header(‘Content-Type: text/html; charset=utf-8’) ?>
is put in in case
<META http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
doesn’t work
and
<META http-equiv=“Content-Type” content=“text/html; charset=UTF-8”>
is put in in case
<?php header(‘Content-Type: text/html; charset=utf-8’) ?>
doesn’t work.

Is that right?

Thanks,

Chris

Basically your server is meant to handle the content type, but because it doesn’t seem to be, Remon suggested the PHP fix. It’s normal to put the meta tag in too, and I’ve heard different reasons why, but I guess it helps in situations such as when you are viewing the pages offline and so no server is sending header information. I’d be happy for someone to give a better explanation, though. :slight_smile:

Ralph,
I’m not getting an error so I’ll leave it. But what is the best order for the diffent types of code? Is there a better order than this:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xml:lang=“en” lang=“en” xmlns=“http://www.w3.org/1999/xhtml”>

<head>

<meta http-equiv=“content-type” content=“text/html; charset=UTF-8” />
<link rel=“stylesheet” href=“/a.css” type=“text/css” media=“screen,projection,tv” />
<title>…</title>
<meta name=“description” content=“…” />
<meta name=“keywords” content=“…” />
<link rel=“shortcut icon” href=“/favicon.ico” />

</head>

Thanks,

Chris

That looks OK to me, although I prefer this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>....</title>
<meta name="description" content="..." />
<meta name="keywords" content="..." />
<link rel="stylesheet" href="/a.css" type="text/css" media="screen,projection,tv" />
<link rel="shortcut icon" href="/favicon.ico" />
</head>

I prefer the title and meta stuff first, before styles etc., but I don’t know how important it is.

Ralph,
I wondered about SEO vs loading speed on that. I had the style sheet reference up high to get the page to load faster but doing that does push the title down one chunk of code. Is that what you were thinking-title is the most important, get it as high as possible?

Thanks,

Chris

The position of the CSS file doesn’t haven’t an impact on loading speed, since it will have to be loaded anyway so it doesn’t matter when it will be loaded. The position will maybe change the loading time of the page a few picoseconds, but it’s really not worth the trouble changing this.
It’s a good idea to put the Content-Type tag first so the browser knows everything in the head is encoded in that type, but other than that I’m pretty sure it doesn’t matter one bit.

As for the whole php vs meta tag for character encoding debate you guys had going earlier, Ralph was correct in everything he said :slight_smile:

Scallio,
So why would one prefer one order of code over another in the <head>, as Ralph favors?

Thanks,

Chris

We all have our likes and dislikes. :slight_smile: It makes sense to me to have the title after the character encoding, followed by any other meta stuff, and then add in styles, then any script links etc. It’s more likely that new style sheets or script links will be added in later, so I like to have them grouped together (although I usually put script links just before the closing </body> tag these days.

Sometimes when people post here you look at the head section of their site and there is a huge mixture of things there … a meta tag, a css file, a few scripts, andother css file link, some more meta data … It just looks like a really big mess to me, and I like to keep things tidy. :slight_smile: