Embed Youtube video in AngularJS generates error when loading the page - javascript

The code snippet below is used to embed Youtube video in my page.
<div ng-if="videoUrl != ''" style="height:400px;">
<iframe width="100%" height="100%" src="{{videoUrl}}"></iframe>
</div>
In my controller, videoUrl is first set to an empty string, and updated once data is retrieved from server.
$scope.videoUrl = '';
videoService.getData().then(function(response) {
$scope.videoUrl = response.videoUrl;
});
The Youtube player works and the URL is correct, I can watch the video with the embeded player after the page is fully loaded. What bothers me is each time when loading the page, an error is returned. I can see from the browser console that it was trying to send a GET request to http://www.example.com/%7B%7BvideoUrl%7D%7D with its status as (canceled). This request is not something I want and it fails for sure.
How can I get rid of that weird GET request?

I modified my code based on RGraham's suggestion and the error is gone now.
First, change src to ng-src and the checking condition to ng-if="videoUrl".
<div ng-if="videoUrl" style="height:400px;">
<iframe width="100%" height="100%" ng-src="{{videoUrl}}"></iframe>
</div>
Second, I initialize videoUrl to undefined.
$scope.videoUrl = undefined;
Now the page loads without sending the previous weird GET request anymore.

Related

Extract data from iframe

I want to extract data from iframe and print to any class using JavaScript
Example:
var iBody = $("#getDataJson").contents().find("pre");
$('#hedl').html(iBody);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id="getDataJson" width="100%" height="100%" src="https://graph.facebook.com/v3.0/me?fields=id"></iframe>
This will result in data in Json format, I want to extract any value from them via JavaScript and print them to any class using
I tried it on a file on the localhost and work without any problems, but when adding a remote link ( https:// ) does not work :(
any suggestions?
thanks in advance!

Changing an iframe SRC dynamically

What I have and I need to do it work properly is when I'm in a "start page" and I click a button like this:
PAGE-1
Point A
It should send me to "another page" where I load dinamically an iframe taking each variable (shopID, type, max-levels and point) from the URL and print it like an iframe this way:
PAGE-2
<iframe id="frame" src="http://myappwebsite/resourcesiframe?shopId=3366&type=image&point=A" width=“XXX" height=“XXX"></iframe>
I've used this javascript snippet but anyway, I can't make it work properly at all.
$(".map-button").click(function (e) {
e.preventDefault();
$("#frame").attr("src", $(this).attr('data - iframe - src'));
});
I only need to get the variables from the data-iframe-src and use them in the iframe, but I'm not able... The problem is that I load elements in different pages, so I don't know how this affect to the URL variables.
I've founded a simple solution for my problem here and it works for me.
First of all I put this simple iframe in my PAGE-2:
<iframe id="frame" src="" width="100%" height="auto" frameborder="0" scrolling="yes"></iframe>
Then in PAGE-1 I've used this type of links:
https://myappwebsite/page-2?shopId=1234&type=image&max-levels=1&point=A
So now my PAGE-2 loads properly with these params in the URL. Then I've used this simple snippet in Javascript to fill my iframe in PAGE-2:
<script type="text/javascript">
$( document ).ready(function() {
$("#frame").attr("src", "https://myappwebsite/resourcesiframe" + window.location.search);
});
</script>
And it works! Now I can go to PAGE-2 with all my params, pick them and use them in SRC inside the blank iframe.
Thanks for getting me in the correct path!

How to get a Youtube embed livestream url using javascript without third party plugins or API?

I'm building a little web page that has an iframe that automatically plays a live stream from a youtube channel using the channel ID.
I need to be able to get the complete URL from that element
(Example Link) without any third party plugins/programs or the API, the purpose of this is to extract the Livestream ID and display the live chat using it.
Here is the relevant code snippet:
<div>
<iframe id="stream" width="560" height="315" src="https://www.youtube.com/embed/live_stream?channel=UCTXma6j0F0IMEdRd4CisrIQ&autoplay=1" frameborder="0" allowfullscreen></iframe>
</div>
<div>
<iframe id="adress" width="560" height="315" src=adress frameborder="0"></iframe>
</div>
<div>
<script>
function search_id()
{
var url = stream.src;
var videoID = url.match(/(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/user\/\S+|\/ytscreeningroom\?v=))([\w\-]{10,12})\b/)[1];
var concat = "https://www.youtube.com/live_chat?v=" +videoID +"&embed_domain=127.0.0.1";
document.getElementById("adress").src = concat;
document.getElementById("test").innerHTML= concat;
}
</script>
<button onclick="search_id()">Get URL</button>
</div>
The var "URL" is the one that should contain the complete URL of the live stream, the current content is just a placeholder, the iframe "address" is the holder for the live chat.
The reason to avoid the API it's because I can't fully comprehend how to use it (I'm attaching it anyway: link), of course, understanding how to get the specific string I'm looking for would be great.

Iframe doesn't resolves the value from controller after the first load

I am using angular js frame work and has an iframe tag inside a ng-dialog. I am passing the src as
var SrcUrl=staticurl+dynamicUrl;
$scope.IframeSrcUrl=$sce.trustAsResourceUrl(SrcUrl);
and at the html
<iframe src="{{IframeSrcUrl}}" name="frame2" id="frame2" frameborder="0" marginwidth="0" marginheight="0" scrolling="auto" onload="" allowtransparency="false" height="350px" width="100%" ></iframe>
The iframe will resolves the proper url on its initial load.. but once if i close the ng-dialog and again open it; it will consider the "IframeSrcUrl" as string take it as a param for my base url and will return a 404 error.
i have tried ng-src instead of src.. but it also didn't works..
here staticurl is an externally hosted site and dynamicUrl is a dynamic genarated parameter. whenever i do some action , for instance a button click i need to start the ng - dialog box which redirects to the IframeSrcUrl via iframe.
now the problem is that , on second load the iframe considers IframeSrcUrl as a string and try to redirect to
"http://localhost:1337/%7B%7BIframeSrcUrl%7D%7D"
On load event of the dialox box, try this :
document.getElementById('frame2').src = document.getElementById('frame2').attr('data-src') and put the attribute data-src={{IframeSrcUrl}} on your iframe.

Unable to change HTML iframe source with Javascript

I'm trying to have a 'video of the day' on my website. I currently have most of this working, and here's how I plan to have it working in the end:
Have a Python script run once every 24 hours to pick the random YouTube video of the day
Write the embedded video URL to a text file
Use Javascript to access the text file and grab the video URL, and use it as the iframe source on my webpage
As I mentioned above, this plan is currently working for the most part. My script is able to pick the random video, and write it to a text file. My web page is then able to pull the video URL from the text file, and write it to the webpage in text form. However, I'm having some troubles having it write the video URL to my iframe.
I did some searching, and people suggested trying the following code:
<iframe id="mainvideo" width="720" height="480" frameborder="0" src="" allowfullscreen></iframe>
<script>
document.getElementById("mainvideo").src =
finalresult + ReturnURL();
</script>
So earlier in my code, I declared the variable 'finalresult' to be the following value: //www.youtube.com/embed/WJDnJ0vXUgw
When I have Javascript write 'finalresult' to my webpage, it will write the video URL as text to the webpage as expected. However, when I try and declare it as the source for the iframe, I end up with a blank iframe on the page.
Does anyone know what the problem is with my code for it not showing this video in my iframe? Any help would be appreciated.
You must be changing the src after any particular event is fired. So try this way,
HTML :
<iframe id="mainvideo" width="720" height="480" frameborder="0" src="http://www.bing.com/" allowfullscreen></iframe>
<button id="changeSrc">Change iFrame</button>
javaScript :
var iframe = document.getElementById("mainvideo");
changeSrc.onclick = function(){
iframe.src = "http://www.w3schools.com/";
};
jsFiddle
Update 1 :
If you want to change the iframe src attribute after page load then go with this way,
window.onload = function(){
var iframe = document.getElementById("mainvideo");
iframe.src = "http://www.w3schools.com/";
};
jsFiddle
If myframe.src does not work for you, then try out myframe.contentWindow.location.href like in this pen.

Categories