Sam Skirrow
Sam Skirrow

Reputation: 3697

jQuery get YouTube video src from iframe code

I am trying to get the youtube src from from within an iframe embed code and pass it as a variable.

For example, if my youtube embed code is:

<iframe width=&quot;1024&quot; height=&quot;576&quot; src=&quot;https://www.youtube.com/embed/rfxESg1_hEA?feature=oembed&quot;frameborder=&quot;0&quot;allowfullscreen></iframe>

I'm trying to get https://www.youtube.com/embed/rfxESg1_hEA

Is there a way to do this with jQuery?

Upvotes: 2

Views: 2436

Answers (1)

FalcoB
FalcoB

Reputation: 1333

Try this:

var src = $("iframe").attr("src").split('?')[0];
alert(src);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<iframe width=&quot;1024&quot; height=&quot;576&quot; src="https://www.youtube.com/embed/rfxESg1_hEA?feature=oembed&quot;frameborder=&quot;0&quot;allowfullscreen"></iframe>

Upvotes: 5

Related Questions