User Tools

Site Tools


en:web_development:media:javascript

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:web_development:media:javascript [2021/12/18 08:50]
mag created
en:web_development:media:javascript [2023/08/16 09:33] (current)
Line 127: Line 127:
   * You may want to add audio when a user makes an action such as a correct (or incorrect) response to a quiz. For this you'd use Javascript to control your audio file.   * You may want to add audio when a user makes an action such as a correct (or incorrect) response to a quiz. For this you'd use Javascript to control your audio file.
   * Let's simplify this by adding a button and playing or pausing the audio whenever the button is clicked.   * Let's simplify this by adding a button and playing or pausing the audio whenever the button is clicked.
-<code>        <input type="button" value="Play Audio" onclick="playPauseAudio()"></code>+<code>        <input type="button" value="Play / Pause Audio" onclick="playPauseAudio()"></code>
   * A button is a type of input.   * A button is a type of input.
   * The <html>value</html> is the text that will be displayed on the button.   * The <html>value</html> is the text that will be displayed on the button.
Line 150: Line 150:
   * Variables in PHP are always represented by <html>$</html>and the variable name.   * Variables in PHP are always represented by <html>$</html>and the variable name.
   * In this case, we have a variable named 'page' and we assign it the value of 'media' which is the page name.   * In this case, we have a variable named 'page' and we assign it the value of 'media' which is the page name.
-  * Now open 'header.php' and replace the <html><script><html> line with the following.+  * Add a command to display the variable. 
 + 
 +<code><?php 
 +    $page = "media"; 
 +    echo $page; 
 +    include("header.php"); 
 +    include("menu.php"); 
 +?></code> 
 + 
 +  * When you save, upload and run the code, you'll see the word 'media' at the top. 
 +  * The code in 'header.php' is part of the same page, so we can use our variable there too. 
 +  * Now open 'header.php' and replace the <html><script></html> line with the following.
 <code>    <script src="<?php echo $page; ?>.js"></script></code> <code>    <script src="<?php echo $page; ?>.js"></script></code>
   * Notice that the only change we've made is to the word 'lists'. We've replaced it with PHP code saying to 'echo' (reproduce in the code) the value of the variable 'page'.   * Notice that the only change we've made is to the word 'lists'. We've replaced it with PHP code saying to 'echo' (reproduce in the code) the value of the variable 'page'.
Line 171: Line 182:
     audio.play();     audio.play();
 }</code> }</code>
 +  * Give that a try before we add the pause option.
 +  * Now, we first want to check the state of the audio (playing or paused) before we decide which action to take.
 +<code>    if (audio.paused) {}</code>
 +  * This code will check whether our audio is <html>.paused</html> and allow us to choose our action based on the result. If true, we want to <html>.play</html> otherwise we want to <html>.pause</html>.
 +  * Can you complete this function without looking at the code below?
 +<code>function playPauseAudio() {
 +    var audio = document.getElementsByTagName('audio')[0];
 +    if (audio.paused) {
 +        audio.play();
 +    } else {
 +        audio.pause();
 +    }
 +}</code>
 +  * We'll save controlling the video by Javascript for a exercise.
 +
 +[[en:web_development:media:flexbox|Next: Flexbox]]
en/web_development/media/javascript.1639846217.txt.gz · Last modified: 2023/08/16 09:33 (external edit)