Welcome Guest, Not a member yet? Register   Sign In
View : full javascript or mix javascript and php
#2

[eluser]heartnetkung[/eluser]
Hi, bastien31
I was having the similar problem as you before. In out case, our web app focus all ui logic in plain javascript (all our view files have no php tag at all).
Here's my solution.
I extend the base controller and create my new $this->loaw->view to inject javascript.
Code:
<?php
class MY_Controller extends CI_Controller {

protected function loadView($view, $data = array(), $return = false) {
  $loadedHtml = $this -> load -> view($view, '', TRUE);
  //sending php data directly to javascript in window.php object
  $insertingScript = '[open script tag]
  var temp2=window.php=window.php||{}; var temp=' . json_encode($data) . '; for(x in temp) temp2[x]=temp[x];[close script tag]';
  $loadedHtml = str_replace('</head>', $insertingScript . '</head>', $loadedHtml);
  if ($return)
   return $loadedHtml;
  else {
   $this -> output -> set_output($loadedHtml);
  }
}
}
Don't forget to replace [open/close script tag].
Here's some example of my javascript code
Code:
$(document).ready(function() {
  if(window.php && window.php.username)
   $('#username-label').text(window.php.username);
})
Hope you like it ;D


Messages In This Thread
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 07:42 AM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 09:21 AM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 09:46 AM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 10:28 AM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 10:37 AM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 11:12 AM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 11:21 AM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 12:15 PM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 12:57 PM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 01:04 PM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 01:07 PM
View : full javascript or mix javascript and php - by El Forum - 04-20-2012, 01:24 PM



Theme © iAndrew 2016 - Forum software by © MyBB