Pages

Memasang CSS dan JS pada CodeIgniter

Menambahkan file JavaScript and CSS (Cascading Style Sheet) sangat mudah. Buat JS and CSS folder di root directory dan copy .js ke folder JS dan .css ke folder CSS.


Untuk contoh asumsikan kamu punya file sample.js dan style.css, Sekarang untuk memasang file tersebut di file view load URL helper sebagai berikut

$this->load->helper('url');

kemudian untuk menghubungkan ke file js dan css tersebut kamu bisa liat contoh berikut

<link rel = "stylesheet" type = "text/css" 
   href = "<?php echo base_url(); ?>css/style.css">

<script type = 'text/javascript' src = "<?php echo base_url(); 
   ?>js/sample.js"></script>

CONTOH
Buar controller dengan nama Test.php kemudian simpan file tersebut di application/controller/Test.php

<?php 
   class Test extends CI_Controller {
 
      public function index() { 
         $this->load->helper('url'); 
         $this->load->view('test'); 
      } 
   } 
?>

buat file dengan nama test.php kemudian simpan file tersebut di application/views/test.php 

<!DOCTYPE html> 
<html lang = "en">
 
   <head> 
      <meta charset = "utf-8"> 
      <title>CodeIgniter View Example</title> 
      <link rel = "stylesheet" type = "text/css" 
         href = "<?php echo base_url(); ?>css/style.css"> 
      <script type = 'text/javascript' src = "<?php echo base_url(); 
         ?>js/sample.js"></script> 
   </head>
 
   <body> 
      <a href = 'javascript:test()'>Click Here</a> to execute the javascript function. 
   </body>
 
</html>

buat file style.css dan simpan di css/style.css

body { 
   background:#000; 
   color:#FFF; 
}

buat file sample.js dan simpan di js/sample.js

function test() { 
   alert('test'); 
}

mudah bukan, semoga bermanfaat

kuyen kuyasakti

Rakyat jelata yang haya ingin berbagi informasi.

No comments:

Post a Comment