specified-target-clientscript

placing scriptFile and cssFile to specified elements, sorting, before and after string such as HTML condition
3 followers

Sometimes we are forced to placing a scriptFile or css file before or after the other scriptFile. On the other we do not want to load scriptFile are not needed, so we do registerCssFile in the relevant view

Requirements

Yii 1.1 or above

Fiture

1.Placing scriptFile and cssFile on target element

2.Local sorting each element

3.Before and after string to render example :

/*$option
 * 'target'=> id element (string)
 * 'order'=> order of render (int)
 * 'before'=> string to render before tag like <!--[if lt IE 9]>
 * 'after'=> string to render after tag like <![endif]-->
 * 'htmlOptions'=> attribut of tag array()
*/
 
$options = array(
  'target'=>'plugin', //or other ID
  'order'=>-1,
  'before'=>'<!--[if lt IE 9]>'
  'after'=>'<![endif]-->'
)

Usage

  1. dowload the extension, extract to /extensions/clientscript
  2. change /config/main.php
//config/main.php
 
'components'=>array(
    ...
    'clientScript'=>array(
        'class'=>'ext.clientScript.SpClientScript',
        ...
    ),
    ...
),

3.add script tag and link tag with ID as target

//layouts/main.php
<!DOCTYPE HTML>
<html>
    <head>
        //meta here
 
        <link id="core"/>
        <link id="plugin"/>
        <link id="page"/>
 
        //other head here
    <head>
 
    <body>
        //body here  
        <script id="core"></script>
        <script id="plugin"></script>
        <script id="page"></script>
    </body>
</html>

4.call registerScriptFile

//set coreScript position
$cs->coreScriptPosition = SpClientScript::POS_TARGET;
 
$cs->registerScriptFile('url',null,$target);
//or
$cs->registerScriptFile('url',null,$options);

5.call registerCssFile

$cs->registerCssFile($url,$media,$target);
 
or
 
$cs->registerCssFile($url,$media,$options);

Change Log

2013-11-28 : all css target fix 2013-07-27 : delete hasCssTarget, change to hasTarget, fixed

2013-07-27 : change syntax :

         registerScriptFile($url,$position,$target);  
         registerCssFile($url,$media,$target);  

         or

         registerScriptFile($url,$position,$options)
         registerCssFile($url,$media,$options);

2013-07-26 : bug, css sorting error, fixed

2013-07-21 : add css manage fiture

2013-07-17 : bug, duplicate url, effect from sorted fiture, fixed

2013-07-17 : add sorted, before string and after string fiture

2013-07-17 : Bug, registered core before rendercore not shown, fixed

Total 1 comment

#14205 report it
Sheillendra at 2013/07/26 05:03am
sorting

Do not use zero if you want to place it at the top, but use negative numbers

Leave a comment

Please to leave your comment.

Create extension