Skip to content

Instantly share code, notes, and snippets.

@ericls
Created May 1, 2015 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericls/1f365b34b18abd9f0792 to your computer and use it in GitHub Desktop.
Save ericls/1f365b34b18abd9f0792 to your computer and use it in GitHub Desktop.
replace youtube url with embeded video
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="style.css" />
<script data-require="jquery" data-semver="2.1.3" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<a href="https://youtu.be/Rr9i16FDl9E" target="_blank">视频</a>
<a href="https://www.youtube.com/watch?v=Rr9i16FDl9E" target="_blank">视频2</a>
<script>
$(document).ready(
function() {
$('a').each(function(i, e) {
url = e.attributes['href'].value;
if (url.indexOf('youtu.be') != -1) {
video_id = url.split('/')[3];
$(e).replaceWith('<iframe width="100%" height="360" src=" https://www.youtube.com/embed/' + video_id + '" frameborder="0" allowfullscreen></iframe>')
} else if (url.indexOf('www.youtube.com/watch') != -1) {
video_id = url.split('=')[1];
$(e).replaceWith('<iframe width="100%" height="360" src=" https://www.youtube.com/embed/' + video_id + '" frameborder="0" allowfullscreen></iframe>')
}
});
}
)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment