Javascript Course

Usually you can't add php code in external javascript (js) file. But, you can add and use php code in the page in which the external javascript file is included.
So, you can apply the following technique to use value of php variable in js file.

1. In the page in which the external javascript file is included, output the value of the php variable to a javascript variable:
<?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.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
How to use php variable in external js file

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142535)
  2. Zodiac Signs PHP code (7232)
  3. The Essene Gospel of Peace (2504)
  4. CSS3 Flexbox Container (1086)
  5. Movie Clip Symbols (2327)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (538)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)