Part of the EllisLab Network
   
 
Script tags => script_tag()
Posted: 19 February 2008 01:24 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  51
Joined  01-10-2008

I’ve extended the html helper by modifying link_tag() function the to help me generate links to scripts. Needless to say, the usage is pretty much the same of link_tag().
The only default parameter is type, which is text/javascript.

Example:

echo script_tag('scripts/tiny_mce.js');
// gives <script  src="http://site.com/scripts/tiny_mce.js" type="text/javascript" /> 

Array example:

$script = array(
          
'src' => 'scripts/tiny_mce.js',
          
'charset' => 'utf-8',
          
'type' => 'text/javascript',
          
'defer' => 'defer'
);

echo 
script_tag($script);
// <script  src="http://site.com/scripts/tiny_mce.js" charset="utf-8" type="text/ecmascript" defer="defer"  /> 

The function:

// ------------------------------------------------------------------------

/**

 * Link

 *

 * Generates link to a Script file

 *

 * @access    public

 * @param    mixed    scripts srcs or an array

 * @param    string    src

 * @param    string    type

 * @param    string    charset

 * @param    string    defer

 * @param    boolean    should index_page be added to the script path path 

 * @return    string

 */    

if (! function_exists('script_tag'))

{

    
function script_tag($src ''$type 'text/javascript'$charset ''$defer ''$index_page FALSE)

    
{

        $CI 
=& get_instance();



        
$script '$v)

            {

                if ($k == '
src' AND strpos($k, '://') === FALSE)

                
{

                    
if ($index_page === TRUE)

                    
{

                        $script 
.= ' src="'.$CI->config->site_url($v).'" ';

                    
}

                    
else

                    
{

                        $script 
.= ' src="'.$CI->config->slash_item('base_url').$v.'" ';

                    
}

                }

                
else

                
{

                    $script 
.= "$k=\"$v\" ";

                
}

            }

            

            $script 
.= "/>\n";

        
}

        
else

        
{

            
if ( strpos($src'://') !== FALSE)

            
{

                $script 
.= ' src="'.$src.'" ';

            
}

            
elseif ($index_page === TRUE)

            
{

                $script 
.= ' src="'.$CI->config->site_url($src).'" ';

            
}

            
else

            
{

                $script 
.= ' src="'.$CI->config->slash_item('base_url').$src.'" ';

            
}

                

            $script 
.= ' type="'.$type.'" ';

            

            if (
$defer    != '')

            
{

                $script 
.= 'defer="'.$defer.'" ';

            
}



            
if ($charset    != '')

            
{

                $script 
.= 'charset="'.$charset.'" ';

            
}

            

            $script 
.= '/>'."\n";

        
}



    

        
return $script;

    
}

 Signature 

Go PHP5

Profile
 
 
Posted: 08 April 2009 03:30 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Avatar
Total Posts:  19
Joined  10-19-2007

Repaired working version, (solving two bugs) (One due to the CHILDISH way this forum handles script tags)

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Script
*
* Generates a script inclusion of a JavaScript file
* Based on the CodeIgniters original Link Tag.
*
* Author(s): Isern Palaus <ipalaus@ipalaus.es>
*            David Mulder <david@greatslovakia.com>
*
* @access    public
* @param    mixed    javascript sources or an array
* @param    string    language
* @param    string    type
* @param    boolean    should index_page be added to the javascript path
* @return    string
*/    

if ( ! function_exists('script_tag'))
{
    
function script_tag($src ''$language 'javascript'$type 'text/javascript'$index_page FALSE)
    
{
        $CI 
=& get_instance();

        
$script '<scr'.'ipt';

        if (
is_array($src))
        
{
            
foreach ($src as $k=>$v)
            
{
                
if ($k == 'src' AND strpos($v'://') === FALSE)
                
{
                    
if ($index_page === TRUE)
                    
{
                        $script 
.= ' src="'.$CI->config->site_url($v).'"';
                    
}
                    
else
                    
{
                        $script 
.= ' src="'.$CI->config->slash_item('base_url').$v.'"';
                    
}
                }
                
else
                
{
                    $script 
.= "$k=\"$v\"";
                
}
            }
            
            $script 
.= "></scr"."ipt>\n";
        
}
        
else
        
{
            
if ( strpos($src'://') !== FALSE)
            
{
                $script 
.= ' src="'.$src.'" ';
            
}
            
elseif ($index_page === TRUE)
            
{
                $script 
.= ' src="'.$CI->config->site_url($src).'" ';
            
}
            
else
            
{
                $script 
.= ' src="'.$CI->config->slash_item('base_url').$src.'" ';
            
}
                
            $script 
.= 'language="'.$language.'" type="'.$type.'"';
            
            
$script .= ' /></scr'."ipt>'."\n";
        }

    
        return 
$script;
    }
}
?> 
Profile
 
 
Posted: 12 May 2009 05:59 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  49
Joined  02-24-2009

I LOVE YOU. <3 Well done mate.

Profile
 
 
Posted: 10 November 2010 05:57 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  1
Joined  11-04-2010

THANKS A LOTT..

Profile