0

I've got a script that goes through the page and returns all selected elements - in this case, <embed...>

For example, when a YouTube video is found in a blog, it returns an iframe. I can take the iframe src, pass it through a function, and regex the video ID. However, when the YouTube video in within a YouTube page, its embedded differently and the video ID is found within the "flashvars" attribute.

Here's an example video embed : http://pastie.org/7155840

What would you guys recommend the best way of confirming the src as YouTube, and getting the video ID qlo5XHZ8-zI from it?

1 Answer 1

2

Here's a solution that doesn't use a regex.

First confirm it as youtube:

if (e.src.substr(0, 19) != "http://s.ytimg.com/") return false

Then extract the video id:

// get the flashvars
var flashvars = e.getAttribute("flashvars")
// convert flashvars string to key-value pairs
flashvars = flashvars.split("&").map(function(entry) {
  return entry.split("=", 2)
})
// look up the value of the "video_id" key
var video_id = flashvars.filter(function(entry) {
  return entry[0] == "video_id"
})[0]

video_id should now be the video's ID.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.