You can use txt.replace('search', 'replace')
to perform String replace and txt.indexOf('search')
to find if the string contains pattern you would like to search for.
This code should work for you:
$("iframe").each(function() {
var src = $(this).attr('src');
if(src.indexOf('https://w.soundcloud.com/player/?url=') != -1 && src.indexOf('visual=true') != -1) {
$(this).attr('src', src.replace('visual=true', 'visual=false'));
}
});
Because assigning new src
actually reloads the iframe, hence creating another HTTP request I've added additional check if this action should be performed with && src.indexOf('visual=true') != -1
. So it now only replaces the src
whenever it's needed leaving other iframes
untouched.