0

Webpagetest.org 告诉我

http://mypage.com/howitworks has CSS in the document body:
Link node http://mypage.com/design_header.css should be moved to the document head.

这个 design_header.css 是在正文中导入的 header.php 的样式表,例如

<body>
 <?php include 'header_login.php'; ?>
</body>

对此我能做些什么吗?

4

1 回答 1

0

我想这可能会告诉您,您的视图需要一个像样的 PHP 模板系统(Smarty?)。

但是,如果您正在寻找快速解决方案,请尝试利用Output Buffering您的需求:

<?php
// at the top of your parent page

function move_css_to_head($buffer)
{
    // do some DOM manipulation here to achieve your goal
    $dom = new DOMDocument;
    $dom->loadHTML($buffer);

    $head = $dom->getElementsByTagName('head')->item(0);
    $body = $dom->getElementsByTagName('body')->item(0);
    $css_elms = $body->getElementsByTagName('link');

    foreach ($css_elms as $elm)
        {
        $head->appendChild($elm->cloneNode());
        $body->removeChild($elm);
        }

    return $dom->saveHTML();
}

ob_start('move_css_to_head'); // buffer response output, and call the assigned function upon flushing
?>

<!-- Regular Page Content -->

<?php
// at the bottom of your parent page, flush the buffer
ob_end_flush();
?>
于 2013-05-18T21:48:03.017 回答