Running a script every time the page loads

I am new to the forum, first question! I'm pretty newbie so it can be very simple ...

I am trying to implement code that will make iframes responsive

(function($) {

$.fn.responsiveVideo = function() {
    // Add CSS to head
    $( 'head' ).append( '<style type="text/css">.responsive-video-wrapper{width:100%;position:relative;padding:0}.responsive-video-wrapper iframe,.responsive-video-wrapper object,.responsive-video-wrapper embed{position:absolute;top:0;left:0;width:100%;height:100%}</style>' );

    // Gather all videos
    var el = $(this),
        all_videos = el.find( 'iframe[src*="player.vimeo.com"], iframe[src*="youtube.com"], iframe[src*="dailymotion.com"],iframe[src*="kickstarter.com"][src*="video.html"], object, embed' );

    // Cycle through each video and add wrapper div with appropriate aspect ratio
    all_videos.each( function() {
        var video = $(this)
            aspect_ratio = video.attr( 'height' ) / video.attr( 'width' );

        video
            .removeAttr( 'height' )
            .removeAttr( 'width' );

        if ( ! video.parents( 'object' ).length )
            video.wrap( '<div class="responsive-video-wrapper" style="padding-top: ' + ( aspect_ratio * 100 ) + '%" />' );
    } );
};
} )(jQuery); 

To activate it, I call it with this code:

$(window).ready(function() {    
 $( 'body' ).responsiveVideo();   
    });

My problem is that it only works on the first page load or when the page is refreshed. When I go to the page containing the iframe, it does not work.

Thank!

+3
source share
1 answer

Try

$(window).load(function() {    
 $( 'body' ).responsiveVideo();   
});

instead

 $(window).ready(function() { 

Hope will help you :)

0
source

All Articles