<?php //php code.. $some_var ='value'; ?> <!doctype html> <html lang='en'> <head> <meta charset='utf-8' /> <title>Page Title</title> <script> var js_var ='<?php echo $some_var; ?>'; </script> </head> <body> Page content.. <script src='external_file.js'></script> </body> </html>
Now, in the script from external_file.js we can use the js_var
(it contains the value of the php $some_var).
2. For example, in external_file.js:
alert(js_var); // value
The javascript variable must be defined before the external js file is included.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net