Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS run() method in IFrame does not rerun on iframe reload

I have a webpage with a "Start Portal" button and an iframe below it (on the same webpage) with id: MyFrame.

I get the URL of my angularjs based portal using jQuery AJAX and set the src of my iframe. Code for loading angularjs based portal is given below. The 'portalURL' parameter contains the URL.

$.ajax({
            method: 'post',
            crossDomain: true,
            contentType: "application/json; charset=utf-8",
            url: webmethodurl,
            data: {},
            dataType: "jsonp",
            jsonp: "JSONCallback",
            success: function(portalURL){
                $('#MyFrame').attr('src',portalURL);
            },
            error: function(e){
                console.log(e);
            }
        });

This works fine and my AngularJS portal open properly in the iframe.

But if I click the "Start Portal" button again, same AJAX code is called and I again get the AngularJS portal URL and it loads again. This works fine, but there is 1 issue.

I have some authentication code written in the AngilarJS run() method:

//Some initializing code before Angular invokes controllers
MyApp.run(['$rootScope', function($rootScope) {
    ValidateSession();                   //1. First validate the session
}]);

function ValidateSession(){
    //this does some validation checks
}

Now, my issue is, this run() method is only called when I click the "Start Portal" for the first time. On subsequent clicks, it do reload the iframe with Portal URL from server side, but the angularjs run() method is never called again.

I tried to manually bootstrap the angular app, but could not make is work properly.

Somehow I want to kill or reload the whole the angularjs app if it was previously loaded and start fresh whenever iframe loads my angularjs portal.

I cannot make any modifications to iframe code, as in production that would be my client's website. I only have to provide them with angularjs portal url, which they will load using similar method I used.

like image 633
Pawan Pillai Avatar asked Feb 03 '26 07:02

Pawan Pillai


1 Answers

In order to prevent the iframe contents being loaded from the cache, you could append a random parameter to the portalURL:

success: function(portalURL) {
    // TODO: detect whether the portalURL already contains query string parameters
    // and use & instead of ?
    portalURL += '?v=' + (new Date()).getTime() + Math.floor(Math.random() * 1000000);
    $('#MyFrame').attr('src', portalURL);
},
like image 167
Darin Dimitrov Avatar answered Feb 04 '26 20:02

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!