0

Can someone kindly show me a javascript that will append this string:

&showinfo=0

to an iframe src attribute but only if the src contains youtube.com

So therefore this tag:

<iframe width="1280" height="720" src="https://www.youtube.com/embed/Yso_Ez691qw?feature=oembed" frameborder="0" allowfullscreen=""></iframe>

Will become:

<iframe width="1280" height="720" src="https://www.youtube.com/embed/Yso_Ez691qw?feature=oembed&amp;showinfo=0" frameborder="0" allowfullscreen=""></iframe>

But only only on youtube urls not other iframes.

0

2 Answers 2

1

The following would select all IFrames that contain string youtube in their src attribute, and append the string &showinfo=0 to its src attribute.

$("iframe[src*='youtube']").each(function() {
    var src = $(this).attr('src');
    $(this).attr('src', src + '&showinfo=0');
});

You may want to tweak it based on your requirements though:

  • For instance, you can check entire youtube URL instead of just 'youtube'.

  • Also, you might want to check if the querystring is not already part of the URL before appending it.

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

Comments

0

Ok lets break this down into steps:

  1. Loops through every iframe on the page
  2. Check if the src of that iframe contains 'youtube'
  3. Update the iframe's src attribute

Here is the code:

$(document).ready(function() {
  // here is the loop
  $('iframe').each(function(i) {
    // here we get the iframe's source
    var src = $(this).attr('src');
    var substring = 'youtube';
    // check if this iframe's source (src) contains 'youtube'
    if(src.indexOf(substring) !== -1) {
      // OK it does - lets update the source (src)
      $(this).attr('src', src + '&showinfo=0');
      console.log($(this).attr('src'));
      // https://www.youtube.com/embed/Yso_Ez691qw?feature=oembed&showinfo=0
    }
  });
});

1 Comment

Really nice. Thanks for sharing this.

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.