Welcome Guest, Not a member yet? Register   Sign In
javascript files with php code
#2

(10-02-2015, 09:02 AM)Lykos22 Wrote: Hi guys, I'd like to ask for some generic information, if possible.

I usually try to put all my javascript stuff in a single file, and not keep them inside <skript> tags in my views, so it would be a little bit cleaner and tided up. However there might be some cases where I have ti embed some PHP code inside my javascript, for instance:

PHP Code:
<script type="text/javascript">
$(function() {
 $.
post('<?php echo base_url($url); ?>', function(data){
 
// some code here
 
});
});
</
script

So in this case I'm just leaving these scripts in my views.

I was wondering whether there's a more efficient way to do this, or another better way to organize all javascripts. Would you recommend another way, or in that case, or keep going this way ?

You can use global variables in JS . Example:

Code:
<script type="text/javascript">
var URL = "<?php echo base_url() ?>"; //base_url is called just one time
$(function() {
$.post(URL+'register', function(data){ //and use the global var URL
// some code here
});
});
</script>

Reply


Messages In This Thread
javascript files with php code - by Lykos22 - 10-02-2015, 09:02 AM
RE: javascript files with php code - by davicotico - 10-02-2015, 12:08 PM
RE: javascript files with php code - by freddy - 10-02-2015, 08:14 PM
RE: javascript files with php code - by JayAdra - 10-02-2015, 08:24 PM
RE: javascript files with php code - by Lykos22 - 10-15-2015, 08:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB