简体   繁体   中英

How to add lines before each and every </head> in multiple HTML Page site?

I hope all are doing well.

How to add respo_style.css before closing </head> in multiple(in my case there is 65 pages) html + PHP page.

is there any best way to add dynamically

<link rel="stylesheet" type="text/css" href="respo_style.css" />

using code?

In my case i have one config.php which is called in every page.

NOTE: Without add or change any other files. Is this possible to add the lines dynamically using jQuery or JavaScript.

Simple things is just add extra additional file in existing without opening each and every files.

I'm sure there are a lot of ways but here's an explanation for the fastest/easiest implementation IMO.

For code that is repetitive, you should store that code in one file and call it using PHP's include_once function. However in order to use PHP in your files, you would need to use use .php extension instead of .HTML (assuming you files are .HTML) for your webpages.

Here's an example:

<head>
<?php include_once("repetitive_code_here.php"); ?> 
</head>

If you run ob_start(); at the beginning of your script, then you can call ob_get_contents() to get the page and replace </head> with your stylesheet.

$string = ob_get_contents();
$link = '<link rel="stylesheet" type="text/css" href="respo_style.css" />';
echo str_replace('</head>', $link . '</head>', $string);
ob_end_clean();

or instead of ob_get_contents pass the funtion to ob_start();

function callback($buffer) {
    $link = '<link rel="stylesheet" type="text/css" href="respo_style.css" />';
    return str_replace('</head>', $link . '</head>', $buffer);
}
ob_start('callback');

but you will need to call ob_end_flush(); at the end.

This will work. I have style.css in all file. so i can include the respo_style.css in style.css using this code @import url("base.css");

You should do something like this:

page.php (write this in all 65 pages)

<?php
require_once('includes/head.php'); //Assuming you put head.php in a folder called "includes" inside the root
?>
//Then the content of the page

And head.php

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="respo_style.css" />
    </head>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM