1

I am using CodeIgniter framework for one of my project. All is going well for me. I have set a config variable in

application/config/config.php

file. How I did that? Have a look below

$config['my_key'] = 'abcdef123';

The issue is when I am trying to use this variable in an external JavaScript file, it is not putting the value there instead of putting all source. The code I used to get the value in JavaScript file is

var mykey = "<?php echo $this->config->item('my_key'); ?>";

but it is not showing the my_key value, but showing

<?php echo $this->config->item('my_key'); ?>

as it is. So how can I achieve the config value in external JavaScript file? I have included the JavaScript file using my controller.

1
  • When you say "External JS File" Are you using a <script scr=""....></script> to include it or are you including it in the view itself. Commented Jul 13, 2017 at 10:16

1 Answer 1

4

You can define this global variable above external js like describe below so you can achieve your goal.

<head>
     <script type="text/javascript">
          var mykey = "<?php echo $this->config->item('my_key'); ?>";
     </script>
     <script src="path/to/external/js/other.js"></script>
</head>

Now in external js you can use above variable value.

other.js

var key = mykey;

Let me know if it not works.

Sign up to request clarification or add additional context in comments.

1 Comment

I am going to answer the same 1up for my side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.