0

I need to append &wmode=transparent to all of my youtube iframe embeds on my blog using javascript.

So...

<iframe src="http://www.youtube.com/embed/7jW_0IQWOQg?rel=0"></frame>

Needs to be changed to:

<iframe src="http://www.youtube.com/embed/7jW_0IQWOQg?rel=0&wmode=transparent"></frame>

So I guess I need to match a youtube embed url, then append it to the end of the url... How can I match the end of the url?

Edit:

I'm using nodejs so there is no DOM.

I'm trying to check though the entire content of a single post from a blog. So the string will contain text/image html and possibly multiple youtube embeds.

3 Answers 3

2

How about something like this:

var iframes = document.getElementsByTagName("iframe"),
    i,
    len,
    src;

for (i = 0, len = iframes.length; i < len; i++) {
    src = iframes[i].src;
    if (src.indexOf("http://www.youtube.com/embed") > -1) {
        iframes[i].src = src + "&wmode=transparent";
    }
}

If you've got no access to DOM (i.e. you're searching a String) then you could use something like:

str = str.replace(/http:\/\/www.youtube.com\/embed\/(.*?)"><\/iframe>/g, function (a, b) {
    return ['http://www.youtube.com/embed/', b, '&wmode=transparent"></iframe>'].join("");
});

Here's a working example.

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

3 Comments

Sorry, I should have mentioned it's in nodejs so no DOM.
No problem. So is <iframe src="http://www.youtube.com/embed/7jW_0IQWOQg?rel=0"></frame> contained within a String?
Yes, it's actually the entire post body from single blog post. So it could have lot's of content and multiple embedded videos.
1

Checkout the uri.js project on Github: https://github.com/medialize/URI.js and http://medialize.github.com/URI.js/ and http://medialize.github.com/URI.js/docs.html#accessors-search

Comments

0

Well, to check if it's a YouTube URL you could simply check if there's "youtube" in it. If there is, you append your parameter. Not sure what you mean by it's possible there may be multiple URLs in the string. However, it is possible that there would not already be any parameters, and thus you would need to use ? instead of &. In that case, just use indexOf to check what you should use.

var iframes = document.getElementsByTagName("iframe");
for(var i = 0; i < iframes ; i++) {
    var iframe = iframes [i];
    if (iframe.src.indexOf("youtube") >= 0)
        iframe.src += "&wmode=transparent";
}

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.