Rich5757 Rich5757 - 4 months ago 11
jQuery Question

Get all Iframe tag inside a textarea having src as http using jquery

I have a textarea with id 'description', User can place as many iframe inside it, I want when user clicks on submit button system scan all Iframes and if there is any iframe which src is http system show an alert message that Iframe src from http site are not allowed. If Iframe src is https then its ok.

I am trying below code

jQuery("form[name=myform]").bind('submit',function(){
var content = jQuery('#description').val();
alert($(content).find('iframe').attr('src','http://'));
});


But its not working.

Please help

Thanks

Answer

Try wrapping you content in a div and then perform find

    jQuery("form[name=myform]").on('submit',function(e){
        e.preventDefault();
        var content = jQuery('#description').val();
        alert($('<div>'+content+'</div>').find('iframe[src^="http:"]'));
        if ($('<div>'+content+'</div>').find('iframe[src^="http:"]').length > 0) {
          alert('Iframes over http aren't allowed ');     
        }
    });
Comments