0

Hi I'm trying to use an external js, the and I'm using Yii clienScript :

Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/js/some.js');

The problem is, in my external js file, I need to pass a url, for my ajax call. and the url is in php, because I'm using the createAbsoluteUrl() below :

$url = Yii::app()->createAbsoluteUrl("/module/controller/action")

How do I pass the $url as a javascript variable to be used by some.js? Thanks!

1 Answer 1

1

You could create a JS object at the end of your PHP code.

   ...
   ...
   $url = Yii::app()->createAbsoluteUrl("/module/controller/action");
   ...
   Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/js/some.js');
   <script>
   var WRAPPER = (WRAPPER || {});
   WRAPPER.url = "<?= $url ?>";
   </script>

Then the javascript file some.js should be able to use this as:

var url = WRAPPER.url;

I have done this on CodeIgniter - I'm assuming Yii works similar.

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

Comments

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.