How to rewrite head to inject JS and or Css – Php


You can 'inject' javascript or css code into a page by either using the <script> and <style> tags within the page, or by generating a dynamic .js and/or .css file using PHP.
 
I mostly use the first option, for example:
 
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">


    <head>


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


        <link rel="stylesheet" href="/css/main.css" />
        <style type="text/css">
            <?php


                echo 'body { background: url("'.$some_image_path_from_database.'"); }';


            ?>
        </style>


        <script type="text/javascript" src="/js/main.js"></script>
        <script type="text/javascript">
            <?php


                echo 'var userid = '.$some_user_data_from_database.';';


            ?>
        </script>


    </head>


    <body>


    </body>


</html>

Tinggalkan Balasan

Please log in using one of these methods to post your comment:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s