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

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 ?
Reply
#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
#3

(This post was last modified: 10-02-2015, 08:17 PM by freddy. Edit Reason: make code )

yeah me too Big Grin

I love too use my js file with single file js not mixing them in php, based on experience i have done like this in view

Code:
<script type="text/javascript">
var products= "<?php echo base_url() ?>admin/products"; //consider it take data products
</script>




then in my js i use this
Code:
url : product,
Reply
#4

(This post was last modified: 10-02-2015, 08:26 PM by JayAdra.)

There are a few different ways to handle this - each have their pros and cons. It depends on preference and your specific requirements really.

This is a great post to refer to for information on each:
http://stackoverflow.com/questions/23740...javascript

I generally do what others have suggested. I have a single <script> element in my <head> which contains all variables I need echoed into JS, which I can then reference anywhere in my other JS files. I use this mostly for language lines, but it has other uses too.
Reply
#5

So basically, you do something like this:

Code:
<head>
<script>
      var url = "<?php echo base_url() ?>admin/products";
      // etc etc
</script>
</head>

<script src="path/to/some/file.js"></script>

// and then in your script in file.js you can access the url
url : url,
etc etc

Am I right ?

but what you do in cases like this, where you need to loop to set dynamically some js data ? See this example:
PHP Code:
'columns': [
       <?
php foreach($array_items as $item): ?>
        { 'data': '<?php echo $item?>' },
        <?php endforeach; ?>
], 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB