1

I have a view file called edit.ctp and a js file called edit.js. On edit.ctp i'm calling edit.js like that:

$this->Html->script('edit', array('inline' => false));

js files its being included right so i have a issue to use php markups like this answer this answer

probably because the file is .js is not able to accept php makups.

So what is the best way to work with php markups on js files?

2 Answers 2

3

Another alternative is to declare the variable globally, and set it from the onload event that runs after the page has loaded:

<script>

var your_variable;

function init() {
  your_variable = document.getElementById('xxx1');
}

</script>

<body onload="init();">

let me know if i can help you more.

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

Comments

0

One thing you can do is just change the name of the js files to edit.php if you do this I would make sure to include:

<?php header("Content-Type: application/javascript"); ?>

While it might not be strictly needed I think it's a good practice.

Your other options would be to change your servers config filter all js through php (from a performance standpoint ... probably not a good idea)

Hope this helps.

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.