Part of the EllisLab Network
   
 
Script tags => script_tag()
Posted: 19 February 2008 12:24 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  52
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 02:30 PM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  11
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 04:59 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  45
Joined  02-24-2009

I LOVE YOU. <3 Well done mate.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120364 Total Logged-in Users: 28
Total Topics: 126489 Total Anonymous Users: 6
Total Replies: 665192 Total Guests: 304
Total Posts: 791681    
Members ( View Memberlist )