1

Suppose I have an external JavaScript file named myJavascript.js which has 3 functions like the following:

function myFunc1(){
    //some code
}

function myFunc2(){
    //some code
}

function myFunc3(){
    //some code
}

Now, I want to add some PHP script in myJavascript.js like the following, and it's from a separate PHP file named myView.ctp:

<?php
$url = Router::url($this->here, true);
$url = explode('/', $url);
$baseURL = $url[0] . '//' . $url[2] . '/' . $url[3] . '/' . $url[5];
$baseURL2 = $url[0] . '//' . $url[2]. '/' . $url[3];
?>

Why I need to add this PHP script inside myJavascript.js is this - I want to use the PHP variables $baseURL and $baseURL2 inside the 3 functions I've created.

How can I achieve this?

Edit:

I'm sorry I actually made a mistake in my question. The php view file is actually named as myView.ctp, as it's a CakePHP view file. The extension is .ctp and not .php. I've updated my question accordingly.

5
  • Can you declare the variables in a <script> element in your myView.php file, and then use those variables in myJavascript.js? Otherwise, I think you'll need to direct your web server to also parse .js files (not just .php files). Seems like it'd also be possible to rename it to myJavascript.js.php to get the web server to handle it as desired. Commented May 13, 2015 at 6:15
  • 1
    you can't use php inside myJavascript.js. you can either use some hidden parameters to access them inside js file or you can make them global. Commented May 13, 2015 at 6:17
  • @irfanrasool that is wrong. You can use PHP code inside javascript files, parse those files via PHP before sending to client and they work just fine as long as their output is a valid JS code. Commented May 13, 2015 at 6:20
  • @Hanky웃Panky do you think that is totally fine. Is that really separation of concerns? Commented May 13, 2015 at 6:22
  • @Capt. Jack Sparrow you could declare global variable in html page using php code and access it in js...if you want I can post an example. Commented May 13, 2015 at 6:35

3 Answers 3

1

It's possible if you work into an internal HTML file and the extension is php. Into your myView.php You can declare a global variable, and assign a response server value, into that file. For example:

myView.php

<script>
    var globalVar = <?php echo "your_value" ;?>
</script>
<script src="external_file.js"></script>

external_file.js

console.log(globalVar) // your_value
Sign up to request clarification or add additional context in comments.

6 Comments

Can you please elaborate? myView.php is basically a HTML file with .php extension.
I'm sorry I actually made a mistake in my question. The php view file is actually myView.ctp, as it's a CakePHP view file. The extension is .ctp and not .php
Downvoted because this is pretty bad practice: w3.org/wiki/JavaScript_best_practices#Avoid_globals I don't see a point why any one would upvote this in the first place for that reason.
This is just an example to show him how to use php into javascript
Even worse then if it just an example, because you encourage people to go for bad practice.
|
0

You cannot use php variables inside a separate javascript file. Instead what you can do is either to include the script inside the php template like

<script type="text/javascript">
....
</script>

or either to parse the url via javascript if this fits for you. You can get the current url with:

window.loacation.href

then you can parse the string as you wish.

Comments

0

I would definitely not do that but instead passing the args to methods or properties.

We usually have one script that takes care of initialization, lazy loading other stuff and so on. This is done in the layout.

<header>
<script src="/js/app.js">
<script>
    app.init(<?php echo json_encode($this->request); ?>);
<script>
</header>

Just make sure you pass only what yo need of the request, it might contain security related data, so be careful by passing the whole thing.

In your specific views:

<script>
    app.nameSpaceForView.someMethod('<?php echo $someStringVar; ?>');
</script>

You can even avoid this by implementing some logic in the above app.init() function that will check if app.controllers.ControllerName.ViewName is set based on the passed request info and if it's present execute it.

You won't need much, in the best case any, JS in your views by this.

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.