I'm writing a script that will pop-up a video and autoplay it following this example. Everything is working fine, however when the user closes the video, it continues playing in the background. How can I tell the browser to turn off autoplay after the user closes the video?
$(document).ready(function() {
$('#headerVideoLink').magnificPopup({
type: 'inline'
});
});
#headerPopup {
width: 75%;
margin: 0 auto;
position: relative;
top: 80px;
}
#headerPopup iframe {
width: 100%;
margin: 0 auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="headerPopup" class="mfp-hide">
<iframe width="854" height="480" src="https://www.youtube.com/embed/qN3OueBm9F4?autoplay=1" frameborder="0" allow="autoplay" allowfullscreen></iframe>
</div>
From my point of view, there is no straight forward answer to your problem. This is a common thing people encounter when trying to show videos on websites.
The best approach to do this, is by using the Bootstrap CSS framework, because it has modals (pop ups) where you can add a video and whenever you close or open them it exposes events that can trigger functionalities.
In this case, you need jQuery to do something when opening and closing the modal. To avoid the video from playing on the background when the user closes the modal, one way would be to remove the content of the src attribute of the iframe, that will prevent the iframe to continue playing the video. And then when the user opens the modal add the link back. If you don't do this, what will happen is that the user may click on the video one time, it will work fine, but then after jQuery removed the link of the video, it wont work.
jQuery script I use for this purpose:
$(document).ready(function(){
// Save the url of the video in a variable
var url = $("#MyVideo").attr('src');
// When the modal is closed remove the url of the iframe
$("#myModal").on('hide.bs.modal', function(){
$("#MyVideo").attr('src', '');
});
// When the modal is opened, add the url to the iframe
$("#myModal").on('show.bs.modal', function(){
$("#MyVideo").attr('src', url);
});
});
You can read more about how Bootstrap modals work here. The details and explanations of the events are on the bottom of the page.
Related
I am a complete novice to coding so please excuse my ignorance. I am embedding a video using the tag which everything works as it should. I was wondering if there is a way to have the controls be hidden when the page loads and have them show up when the mouse is hovered over the video; like it does once it is playing. Any way to do this?
Btw: this is being entered into an html content box in a document editor (BEE)
THANK YOU
EDIT: attached are pictures of what it looks like where I can put the code. Again, I have minimal experience with this stuff. It is an html content box in a document editor.
What html box selection looks like
Once html box is added to editor
Taken from: HTML5 video - show/hide controls programmatically
<video id="myvideo">
<source src="path/to/movie.mp4" />
</video>
<p onclick="toggleControls();">Toggle</p>
<script>
var video = document.getElementById("myvideo");
function toggleControls() {
if (video.hasAttribute("controls")) {
video.removeAttribute("controls")
} else {
video.setAttribute("controls","controls")
}
}
</script>
I have a iframe in my web page of a video from another site but when i click play or anywhere in the iframe space it opens a link to the site with the video of it.
I'm trying to make a site for playing series and i want to use iframe to show the video from another site rather than downloading it.
I've tried everything but i viewed the other sites source code and it seems it has some java code i suppose that's causing the issue preventing the iframe from working.
http://entervideo.net/watch/94c96f7dc0bd33c
that's the link above, could you kindly inspect the source code first.
This is the code that I tried:
<iframe style ="display:block; margin: 0 auto;" width="1238" height="696"
allowfullscreen="" webkitallowfullscreen="" mozallowfullscreen=""
frameborder="0" src="http://entervideo.net/watch/94c96f7dc0bd33c"
scrolling="no" target="_self"></iframe>
(Down below is the java script code from the link and I think it's causing the problem)
<script>
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
console.log(document.referrer);
console.log(inIframe());
//if(!document.referrer&&inIframe()) document.write('<a
href="http://entervideo.net/watch/94c96f7dc0bd33c" target="_blank"
style="position:fixed; display:block; height:100%; width:100%; z-index:99999;
top: 0;left: 0; background: rgba(255, 255, 255, 0.1);color:#fff;font-
size:30px;">Please remove sandbox tags on the iframe.</a>');
if(document.referrer||!inIframe()){
var element = document.getElementById("ptc");
element.parentNode.removeChild(element);
console.log('deleting');
}
</script>
i want it to play the video on my web page when i click on the play button on the control rather than it opening the link to the other site.
Replace target="_blank" with target="_self" in JavaScript.
Google Chrome is now shipping with a download button for videos that are just embedded videos (i.e. not MSE):
I'm having a hard time find any documentation for Chrome's implementation of the <video> tag. Does anyone know if there is a way - short of disabling "controls" and creating your own video player controls - of disabling this feature?
I realize that if this is showing, it's already easy to download the video, I just want to disable that functionality from appearing as part of the controls.
Thank you!
or you can simply add nodownload in controlsList
<video width="512" height="380" controls controlsList="nodownload">
<source data-src="mov_bbb.ogg" type="video/mp4">
</video>
You can inspect the controls of the native Chrome Video Player by activating the shadow DOM in Settings|Preferences -> Elements -> Show user agent shadow DOM
After that you can inspect the players buttons.
Now the problem is that the download button cannot be accessed via CSS for some reason.
video::-internal-media-controls-download-button {
display:none;
}
won't work.
Even selecting the preceding button and targeting its neighbor using + or ~ won't work.
The only way we found yet was nudging the button out of the viewable area by giving the control panel a greater width and making the enclosure overflow: hidden
video::-webkit-media-controls {
overflow: hidden !important
}
video::-webkit-media-controls-enclosure {
width: calc(100% + 32px);
margin-left: auto;
}
I hope google will fix this issue soon because most content providers won't be happy with this...
Demmongonis solution does work but be aware it can lead to unwanted results.
Android/Chrome sometimes, depends in the video I guess and other factors, adds buttons at the right of the download-button. i.e. the casting-button (there is no way to select it). It will make the download-button to remain visible and the last button to get hidden (casting-button)
Update
It is posible now to hide the download button using the controlsList attribute:
<video controlsList="nodownload" ... />
Yes, this is possible now, at least at the time of writing, you can use the controlsList attribute:
<video controls controlsList="nodownload">
<source data-src="movie.mp4">
</video>
It seems this was introduced in Chrome 58, and the documentation for it is found here: https://developers.google.com/web/updates/2017/03/chrome-58-media-updates#controlslist
Developers can now customize media controls such as the download, fullscreen and remoteplayback buttons.
Usage in HTML:
<video controls controlsList="nofullscreen nodownload noremote foobar"></video>
There is even an official sample page: https://googlechrome.github.io/samples/media/controlslist.html
One more control item I was trying to disable, additionally to 'download' - is 'picture-in-picture'.
Sadly there`s no property, for that purpose to be added in the controlsList. But there is an attribute - disablePictureInPicture you can add to the Element to disable pip.
Example disabling both download and picture-in-picture:
<video disablepictureinpicture controlslist="nodownload">...</video>
Details: https://wicg.github.io/picture-in-picture/#disable-pip
Hey I found a permanent solution that should work in every case!
For normal webdevelopment
<script type="text/javascript">
$("video").each(function(){jQuery(this).append('controlsList="nodownload"')});
</script>
HTML5 videos that has preload on false
$( document ).ready(function() {
$("video").each(function(){
$(this).attr('controlsList','nodownload');
$(this).load();
});
});
$ undevinded? --> Debug modus!
<script type="text/javascript">
jQuery("video").each(function(){jQuery(this).append('controlsList="nodownload"')});
</script>
HTML5 videos that has preload on false
jQuery( document ).ready(function() {
jQuery("video").each(function(){
jQuery(this).attr('controlsList','nodownload');
jQuery(this).load();
});
});
Let me know if it helped you out!
To keep it simple.. You need to add an attribute called controlslist (LOWERCASE, directly after controls) and you need to set its value to ="nodownload". Also, make sure your src file(type) and your attribute type's value match, unlike some of the above examples; my link is to a file named 'sunrise over water.mp4' on my Google Drive. How I do it looks like this:
<video src="https://drive.google.com/open?id=0B1CDu1eNPJqDVEQxMzZUV1dURjg" title="sunrise over water" width="420" height="300" controls controlslist="nodownload" type="video/mp4">
Video Not Supported By Your Browser...
</video>
OR
<video width="440" height="320" title="sunrise over water" controls controlslist="nodownload">
<source src="https://drive.google.com/open?id=0B1CDu1eNPJqDVEQxMzZUV1dURjg" type="video/mp4">
Video Could Not Be Played In Your Browser... Sorry.
</video>
In addition to above answers you have to add following code to disable context menu:
index.html: (globally)
<body oncontextmenu="return false;">
OR you can disable context menu for some element:
element.oncontextmenu = function (e) {
e.preventDefault();
};
Plain javascript to disable the "download" Button from a video in your page:
<script>
window.onload = function() {
video = document.querySelector('video');
if (video) {
video.setAttribute("controlsList", "nodownload");
}
};
</script>
If you want to, you can also is querySelectorAll and remove each video. In my example I just have only one video per page.
The above answer offers a good solution. However, when I was working on this in my project, there were two problems with it.
Download occurs (as if the download button had been pressed) when right margin area of the fullscreen button is touched on Android (mobile or tablet). Applying z-index didn't fix it.
Because of overflow:hidden, the download button is invisible but still exists to the right of the fullscreen button. That means when you press "tab" several times after clicking any control button or bar on PC, you can still reach the download button.
Additionally, be careful -- some small-width devices (e.g. mobile phones) are small enough to hide the seek bar. It would need many more pixels to hide the download button.
I hope Google provides the option to adjust this ASAP.
I using following JavaScript snippet which is working very well:
document.querySelectorAll("video[id^=media-player]").forEach((elem) => elem.controlsList.add("nodownload"));
Example: www.ring-cafe-finsterwalde.de/archiv/archiv.html#archiv4
I have an iframe where the web page inside of the iframe runs a JavaScript redirect to a new page. On most browsers, the iframe is redirected to the new page. On Chrome however this script acts as a "frame breaker" and the entire page gets redirected. How do I make chrome act like the other browsers?
To Reproduce this effect on your machine:
Open this page: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_iframe_height_width
Then replace the iframe source with this page:
src="http://www.tizag.com/javascriptT/examples/timedelay.php"
Chrome supports the sandbox attribute which you can use to prevent the loaded page from breaking out of your iframe.
If you add sandbox="allow-scripts" to the iframe in your example it works on Chrome.
You could get the link of the redirect page and put it in. For example, if www.go.ogle.com/1gsth7uh redircted you to armorgames.com, just put in armorgames.com instead of www.go.ogle.com/1gsth7uh.
You could do this instead of whatever you use for the method:
<button onclick ="javascript:ShowHide('iframe')" href="javascript:;" >Show/Hide Iframe</button>
<iframe id="iframe" scr="https://secure.translations.com/ProjectA" style="DISPLAY: none" />
<script language="JavaScript">
function ShowHide(divId)
{
if(document.getElementById(divId).style.display == 'none')
{
document.getElementById(divId).style.display='block';
}
else
{
document.getElementById(divId).style.display = 'none';
}
}
</script>
I have a web-page that contains a hidden <div> using display: none; and I have a button on the page, that when clicked will change the visibility of the <div>, and overlay it on top of everything else (because it has a z-index set).
Within this <div>, I have a Google Map embedded using an iFrame with the Google Map pin dropped on the location I am trying to show to my users.
The problem
Because the Google Maps iFrame is loading on the page load and while the <div> is hidden, it means that when the <div> is shown the Google Map is not aligned properly (the pin and central location are now in the top left hand corner)
The solution I am looking for
I know that some people are probably going to tell me ways in which I "should" recode my entire page. What I am actually looking for is some sort of onClick function I can set that will reload the iFrame so that the map is properly centered.
Things to know
This iframe has a Google Maps page as its src. i.e. a URL rather than a link to a file in my site.
Any help would be greatly appreciated! A lot of code I have looked at searching the net seems to work at refreshing a specific file that is referenced rather than an external URL.
Would it work if I embedded the map in another HTML file, and then placed that HTML file as the frame source?
I had this similar issue and solved it by changing the css style of the div through jquery, and changing the place where I put the iframe of google map.
The problem
The jquery code:
<script type="text/javascript">
$(function(){
$("#map_link").click(function(event) {
event.preventDefault();
$("#map").slideToggle();
});
});
</script>
The HTML:
I had the link and the google map iframe loaded inline into a div with a display:none css style. Because a display:none style makes the div width:0 and height:0, the address requested in google map doesn't display properly.
<a id="map_link" href="#">See map.</a>
<div id="map" style="display:none">google_map_iframe_here</div>
The solution
The jquery code
<script type="text/javascript">
$(function() {
$("#map_link").click(function(event) {
event.preventDefault();
$("#map").slideToggle();
$("#map").html('google_map_iframe_here').css('display','block');
});
});
</script>
The HTML: The div where I used to put the map now is empty, because I load the map only when clicking on the link, and at that moment I change the css style from display:none to display:block, so the map shows well.
<a id="map_link" href="#">See map.</a>
<div id="map" style="display:none"></div>
Hope this helps!
Have a good coding day!
I'm no pro, but I removed the onload="initialize()" from the body tag and changed it to onclick="initialize()" in the button that unhides the div. This seems to work now.
I'm not using an iframe (I'm using version 3 of the Google Maps API), but just had the same "not aligned properly" issue due to a 'hidden' div. My fix, instead of using display: none, which removed it from the DOM entirely, I used visibility, and height like so:
.myMapDiv {
visibility: hidden;
height: 0px;
}
I believe what's happening is that because 'visibility: hidden' actually keeps the element in the DOM still, the map is able to render as intended. I've tested it out in FF/Chrome/IE 7-9 and seems to be working so far without issue.
After hours of searching on the Internet and getting no results I decided to try what I put in as my final side note on my question and it worked!
I simply made a second file called map.html and inside the code was literally:
<html>
<iframe> </iframe>
</html>
with obviously the Google Maps source and then in my main page I had the src for the iframe linked to pages/map.html instead of the Google Map link.
instead of hiding the div with display:none, use something like position: absolute; top: -9999px; left: -9999px
then, when you want to show the content for that div, set those properties to position: static; top: auto; left: auto or something like that
You can simply refresh the iframe like this:
var myIframe = jQuery('#myIframe');
myIframe.attr('src',myIframe.attr('src')+'');
I had the same problem, my solution was to set both the div & the iframe to 0px height and then have it changed to the desired height when toggled.
<script language="javascript">
function toggle() {
var ele = document.getElementById("toggleText");
var ifr = document.getElementById("iframe");
var text = document.getElementById("displayText");
if(ele.style.visibility == "visible") {
ele.style.visibility = "hidden";
ele.style.height = "0px";
ifr.style.height = "0px";
text.innerHTML = "<img src='#' border='0' width='180' height='65'>";
}
else {
ele.style.visibility = "visible";
ele.style.height = "420px";
ifr.style.height = "420px";
text.innerHTML = "<img src='#' border='0' width='180' height='65'>";
}
}
</script>
<div id="mb">
<a id="displayText" href="javascript:toggle();"><img src="#" border="0" width='180' height='65'></a>
</div>
<div id="toggleText" style="visibility: hidden; height: 0px;">
<p><iframe id="iframe" style="height: 0px;" src=#" width="650">
</iframe></div>
<script type="text/javascript">
$(document).ready(function (){$("#showMap").slideUp();})
</script>
<script type="text/javascript">
function showMap()
{
$("#showMap").slideToggle();
}
</script>
Show / Hide Map
<div id="showMap" style="margin-left:15px; width:615px; height:400px;">
<?php require_once "map.php";?>
</div>
There is a solution using both css and jQuery.
First you need a wrapper div without height which will include the iframe.
In this way the iframe will load normally without be visible at all.
Next using jQuery you can display/hide the iframe.
HTML
<div id="map-show">Show Map</div>
<div id="map-hide">Hide Map</div>
<div id="map-wrapper" style="height:0; overflow:hidden;">
<div id="gmap">
<iframe width="850" height="650" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="xxx"></iframe>
</div>
</div>
CSS
.hidden {
display: none;
}
jQuery
$(document).ready(function() {
$('#map-show').on('click',function(){
// Remove class hidden from map - Display map
$('#gmap').removeClass('hidden');
// Set height to map container div - ONLY one time needed
$('#map-wrapper').css('height', 'auto');
});
$('#map-hide').on('click',function(){
// Add class hidden to map - Hide map
$('#gmap').addClass('hidden');
});
});
I have been struggling with something like this as well. Where I initially add a class .hidden which is display: none;. But when I toggle .hidden, the map is not centered. I found that waiting until the map is fully loaded, using the idle event listener before adding the class .hidden solved all my display issues.
google.maps.event.addListenerOnce(map, 'idle', function(){
// do something only the first time the map is loaded
$mapContainer.addClass('hidden');
});
reference:
How can I check whether Google Maps is fully loaded?
You could use visibility hidden and use jQuery to show and hide it.
$('click-button').click(function(){
var visibility = $("hidden-div").css("visibility");
if (visibility == "hidden")
$("hidden-div").css("visibility","visible");
else
$("hidden-div").css("visibility","hidden");
});
Here is a solution that does not require any programming at all!
When getting the embed code, click "customize and preview embedded map", and then just drag the map down and to the right so that the push pin is in the lower right corner and then grab the new embed code.
When the map centers in hidden mode, it will still come up correctly when expanded. (not perfect I know, but if all you really want is for the push pin to show on the map, totally works)
Same issue, easy solution.
css hidden leave a space in html flow. I don't know if a css position outside the screen is well accepted on every device and get a block perfectly hidden.
jquery is nice working with block width and height.
css:
#map {overflow:hidden; float:left;}
html:
show
hide
<div id="map">
<iframe width="100%" src="http://maps.google.com/maps f=q&source=s_q&hl=en&geocode=&ie=UTF8&iwloc=A&output=embed ...>
</div>
javascript:
var w=sames as gmap width;
var h=sames as gmap height;
$(document).ready(function(){
$("#map").width(0);
$("#map").height(0);
$("#btn_show").click(function(){
$("#map").show();
$("#map").width(w);
$("#map").height(h);
});
$("#btn_hide").click(function(){
$("#map").hide();
});
})
I encounted the same problem.
Solution
Using iframe as an external source works.
online demo
http://jsbin.com/nogomi/1
Make sure that iframe element's name attribute is the same as a element's target attribute
Inspired from https://developers.google.com/maps/documentation/javascript/
in CSS:
#googleMap { visibility: hidden; }
Original
beim Klick der die Karte öffnen soll jQuery:
EDIT
jQuery when you click to open the map:
$('#googleMap')
.css('display','none')
.css('visibility','visible')
.fadeIn(500);
});