Welcome Guest, Not a member yet? Register   Sign In
Script tags => script_tag()
#1

[eluser]fdog[/eluser]
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:
Code:
echo script_tag('scripts/tiny_mce.js');
// gives <script  src="http://site.com/scripts/tiny_mce.js" type="text/javascript" />


Array example:
Code:
$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:
Code:
// ------------------------------------------------------------------------

/**

* 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;

    }

}
#2

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

Code:
<?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 <[email protected]>
*            David Mulder <[email protected]>
*
* @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;
    }
}
?&gt;
#3

[eluser]Fr3aked0ut[/eluser]
I LOVE YOU. <3 Well done mate.
#4

[eluser]Unknown[/eluser]
THANKS A LOTT..
#5

[eluser]Bill Hernandez[/eluser]
Made some small changes to remove spaces whose syntax did not conform with syntax checker.

Modified: added a boolean flag to accommodate html5 syntax

SEE: http://www.tutorialspoint.com/html5/html5_syntax.htm

MODIFY : /application/config/autoload.php
make sure the extended helper is loaded first such as 'MY_html' before 'html', otherwise you will get a loading error

$autoload['helper'] = array('url', 'MY_html', 'html', 'form');

Code:
&lt;?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 <[email protected]>
*            David Mulder <[email protected]>
*
* Modified to accomodate html5 syntax
* SEE: http://www.tutorialspoint.com/html5/html5_syntax.htm
*
* MODIFY : /application/config/autoload.php
*    make sure the extended helper is loaded first such as 'MY_html' before 'html',
*    otherwise you will get a loading error
* $autoload['helper'] = array('url', 'MY_html', 'html', 'form');
*
* @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
* @param    boolean  type
* @return   string   html5
*/

if ( ! function_exists('script_tag'))
{
    function script_tag($src = '', $language = 'javascript', $type = 'text/javascript', $index_page = FALSE, $html5 = true)
    {
        $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-&gt;config-&gt;site_url($v).'"';
                    }
                    else
                    {
                        $script .= ' src="'.$CI-&gt;config-&gt;slash_item('base_url').$v.'"';
                    }
                }
                else
                {
                    $script .= "$k=\"$v\"";
                }
            }

            $script .= "></scr'.'ipt>\n";
        }
        else
        {
            if ( strpos($src, '://') !== FALSE)
            {
                $script .= ' src="'.$src.'"'; // removed extra space - Bill Hernandez(Plano, Texas)
            }
            elseif ($index_page === TRUE)
            {
                $script .= ' src="'.$CI-&gt;config-&gt;site_url($src).'"'; // removed extra space - Bill Hernandez(Plano, Texas)
            }
            else
            {
                $script .= ' src="'.$CI-&gt;config-&gt;slash_item('base_url').$src.'"'; // removed extra space - Bill Hernandez(Plano, Texas)
            }

            if(false == $html5)                     // fixed a bug - Bill Hernandez(Plano, Texas)
            {
                $script .= ' language="'.$language; // added extra space - Bill Hernandez(Plano, Texas)
                $script .= '" type="'.$type.'"';
            }

            $script .= '></scr'.'ipt>'."\n";        // removed extra space - Bill Hernandez(Plano, Texas)
        }

        return $script;
    }
}
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB