2

How can I modify the URL of an iframe, if it contains the youtube.com text?

I want to append:

&wmode=Opaque

to it, but only if this argument doesn't already exist;

Tried:

$('iframe[src*=youtube.com]').attr('src',
   $(this).attr('src') + '&wmode=Opaque');`

but I get an error:

Uncaught Error: Syntax error, unrecognized expression: [src*=youtube.com]


ok, I found out:

$('iframe[src*="youtube.com"]').each(function(){
  this.src += '&wmode=Opaque';
});
3
  • 1
    You get a syntax error because you need to quote the value: src*='youtube.com'. Commented Jan 22, 2012 at 13:02
  • With your last code you add '&wmode=Opaque' to the URL even if that param already exists. Commented Jan 22, 2012 at 13:15
  • you are right, do you know how can I check if it exists? is there some strpos function in javascript? Commented Jan 22, 2012 at 14:41

2 Answers 2

1

According to the jQuery API you want to do this:

$('iframe[src*=youtube.com]').attr('src',$(this).attr('src') + '&wmode=Opaque');
$('iframe[src*="youtube.com"]').attr('src',$(this).attr('src') + '&wmode=Opaque');

Note the double quotes around 'youtube.com'.

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

Comments

1

I would do it in this way:

<script type="text/javascript">

    function $_GET(key, src)
    {
        var re = new RegExp('[&|?]'+ key +'=([^&]*)','i');
        return (src = src.replace(/^\?/,'&').match(re)) ? src=src[1] : src='';
    }

    function appendToIframe(id, key, value)
    {
        var iframe = document.getElementById(id);
        var src = iframe.src;

        // Append desired key and value if key doesn't exist
        if (!$_GET(key, src))
        {
            var glue = (src.indexOf('?') == -1) ? '?' : '&';
            src += glue + key +'='+ value
        }

        iframe.src = src;           
    }


    </script>

<iframe src="your_url" id="iframeID"></iframe>

<a href="#" onclick="appendToIframe('iframeID', 'wmode', 'Opaque'); return false;">APPEND</a>

Just change src and id of the iframe tag as desired, and then use the function appendToIframe to append a new couple of key and value to the iframe URL only if key is not already present.

Please note that jQuery is not needed for that.

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.