Skip to content
Justin Campo edited this page Oct 24, 2015 · 1 revision

In order to dynamically utilize javascript (no inline javascript should be present) you can utilize the Page Class javascript system

##Usage Should only be used in controllers and views

$c->page->data["js"][] = "jquery-combobox.min.js"; //will load files that are in the js folder 
$this->page["script"][] = "alert('in');"; //allows you run raw javascript
$this->page["script"][] = array("action" => "click", "id" => "exampleID", "value" => "alert('click');"); //preconfigured actions/javascript functions

##Explanation Page data is utilized in the page->render() method by including ./foot_js.php

if (isset($this->data["js"]) AND is_array($this->data["js"]) AND count($this->data["js"]) > 0){
    foreach ($this->data["js"] as $jsFile){
        echo '<script src="'.$c->config->js.$jsFile.$this->cache_bust("js").'"></script>';
    }
}
if (isset($this->data["script"]) AND is_array($this->data["script"]) AND count($this->data["script"]) > 0){
    echo '<script>';
    foreach ($this->data["script"] as $jsScript){
        if (is_array($jsScript) AND isset($jsScript["id"]) AND isset($jsScript["value"])){
            if ($jsScript["action"] == "click"){
                echo '$("#'.$jsScript["id"].'").click(function() {'.$jsScript["value"].'});';
            }
        } else {
             echo $jsScript;
        }
    }
    echo '</script>';
}
Clone this wiki locally