I am not experienced in Javascript, I have the following script to play video files on Andriod phone, and it works fine.
<script type="text/javascript">
function PlayMyVideo(arg) {
var myVideo = document.getElementById([arg]);
myVideo.play();
}
</script>
<video id="what" src="what.mp4" poster="" />
<input type="button" onclick="PlayMyVideo('what')" value="Play" />
I am trying to write the tag on the fly:
<script type="text/javascript">
function PlayVideo() {
new_video = document.createElement('video');
new_video.setAttribute('scr', 'what.mp4');
new_video.play();
}
</script>
<input type="button" onclick="PlayVideo()" value="Play2" />
Nothing happen, would appreciate your suggestions.
Thanks in advance
new_video.setAttribute('scr', 'what.mp4');
'scr' is misspelled. It should be 'src'.
and also you should wait for the movie to load before play
Well you're not appending the newly created tag to anything, so it can't play because it's in "memory"/"void", not on the screen.
<div id='plc'> </div>
<script type='text/javascript'>
function PlayVideo() {
new_video = document.createElement('video');
document.getElementById('plc').appendChild(new_video);
new_video.setAttribute('scr', 'what.mp4');
new_video.play();
}
you are creating the video element, you need to add it to the DOM before it will be visible, more info here: http://www.javascriptkit.com/javatutors/dom2.shtml
Related
I'm new to ASP.NET Core and trying to figure out how to display a Bootrstap modal based on a string property of the underlying page Model (Model.Message). I've tried several different things and nothing is working. Below is a sample Bootrstap modal I found on the Bootstrap website. I can get the modal to display with a button, but that's not the functionality I am looking for.
<!-- Modal -->
<div class="modal fade" id="UserDialog" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
EDIT: Added code to my scripts section at the bottom of the page and a new JavaScript function in an external .js file.
Here's my scripts section on the Razor page
#section scripts
{
<script>
$(document).ready(function () {
ShowModalDlg();
var message = '#Model.Message';
if (message != "") {
ShowModalDlg();
}
});
</script>
}
Here's the new JavaScript function in my external file
function ShowModalDlg()
{
$("#UserDialog").modal("show");
}
The ShowModalDlg() function does get called when it is supposed to, but the bootstrap modal is not showing. The button I added to test "showing" the modal still works. I also added (although not shown here) a temporary call to alert("I'm here!") inside of the ShowModalDlg() function and the alert shows perfectly. I did just check the Chrome debug console and I am getting an error that seems to indicate I am loading jquery twice. That error message is:
"$(...).modal is not a function"
So it looks like I need to figure out where that's happening.
I have the following in my _Layout.cshtml page:
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
If I understand you, this kind of approach might solve your problem. We define a ShowModal variable sent from the controller as a javascript variable, then we trigger the modal by checking the value of this variable.
EDIT: We expect 'show' string as showModal variable from controller.
<script>
var showModal = '#Model.showModal';
if(showModal == 'show'){
$("#exampleModal").modal("show");
}
</script>
If you use it this way it will work. You can check it here.
<script src="https://cdn.jsdelivr.net/npm/jquery#3.6.0/dist/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <!-- You may not really need this. -->
<!-- Bootstrap 5 (https://getbootstrap.com/docs/5.0/getting-started/download/) -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
I fixed the problem by combining the answer above with the following:
$(window).load(function ()
{
var message = '#Model.Message';
if (message != "") {
ShowModalDlg();
}
});
I should not have had that code inside of document ready, which is why it wasn't working and also causing the error message.
I’m using “QDate” Quasar component.
I need to handle event when user clicks on the date. QDate provides an event named "#input " but this event is only triggered when model changes. But what if user clicks on already selected date. How to capture the event in that case?
Can anyone help me on this?
Thanks
in quasar v1 example
#input="() => $refs.qDateProxy.hide()"
in quasar v2 vue 3 maybe use
#update:model-value -> function(value, reason, details)
Documentation
https://quasar.dev/vue-components/date#basic
QDate API - click on events
I didn't found any problem with the event mentioned! in case still have the problem let's know your quasar version and vuejs too.
const app = new Vue({
el: '#root',
data: {
message: 'كيف حالك اليوم؟',
date: '2019/02/01',
}
});
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/quasar#1.15.23/dist/quasar.min.css" rel="stylesheet" type="text/css">
<script src="https://cdn.jsdelivr.net/npm/vue#^2.0.0/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/quasar#1.15.23/dist/quasar.umd.min.js"></script>
<div id="root">
<h5>{{ message }}</h5>
<div class="flex">
<div class="">
<q-date v-model="date" #input="$q.notify('date clicked')" />
</div>
</div>
</div>
Tested configuration: quasar#1.15.23
The audio tag works fine when i open the page via router-link(from another page), however if i refresh this current page with audio tag, audio didn't reload, is there anything that i need to add or change? or i should try other method than audio to play audio files?
Below are my code:
Should I try another method to play .mp3 files?
<template lang="pug">
audio#player(autoplay="" loop="")
source(src="#/assets/alertsound.mp3" type="audio/mp3")
</template>
I am not really sure whats causing this exactly but there is another way you can play your audio ...this will trigger the audio load() :
new Vue({
el: "#app",
mounted: function() {
this.$nextTick(() => {
this.$refs.audio.load()
})
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div>
<audio ref="audio" autoplay loop>
<source src="https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg"
type="audio/ogg"
>
</audio>
</div>
</div>
I've been trying to call a javascript function from a button click, and I can't figure out what I'm doing wrong.
I've searched for similar questions and followed the answers, but the button click is still not calling the function.
The following code is in my .cshtml page. First, I create the button
<input type="button" id="myButton" onclick="Save()"/>
And I have a function on the same page
<script> function Save() { ..do stuff here } </script>
This function never gets called. What am I doing wrong?
Here you can do like this way:
$(document).ready(function() {
$("#myButton").click(function() {
alert("button clicked.");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="myButton" value="test"/>
What I have currently will display my poster once the video ends. However, it also shows at the beginning of the video (which is set to autoplay). I don't want it at the beginning of the video, just the end. Here's what I have.
<div id="wrapper">
<video id="example_vid" class="video-js vjs-default-skin"
controls preload="auto" poster="images/poster_test.png"
width="780" height="439"
autoplay
data-setup='{"example_option": true}'>
<source src="videos/subaru.mp4" type='video/mp4' />
<source src="videos/subaru.webm" type='video/webm' />
<source src="videos/subaru.ogv" type='video/ogg' />
</video>
</div>
…
<script>
var vid = videojs("example_vid");
vid.on("ended", function(){
vid.posterImage.show();
vid.currentTime(0);
})
</script>
Try this. It worked for me.
<script>
var vid = document.getElementById("myVideo");
vid.addEventListener('ended', function(){
vid.poster = "images/poster_test.png";
vid.src = "";
vid.controls = false;
});
</script>
I didn't test this, but I'd try 1) taking the poster attribute out of the video element, then 2) setting it through script on ended. Like this
<script>
var vid = videojs("example_vid");
vid.on("ended", function(){
vid.poster("images/poster_test.png");
vid.currentTime(0);
})
</script>