1

What I basically need is to get the url of the window that contains the iframe in which I'm currently with javascript, I've done almost everything I've seen on google and nothing works for me, I'm doing a client webpart and I need the url in an anchor that redirects me back after see or do some stuff in other page, if someone can bring me some help with it or if there is another way more easier to do it I'll apreciate it a lot.

thanks in advance!

1
  • 1
    Have you looked at postMessage? See this question Commented Jul 12, 2016 at 7:09

1 Answer 1

2

From app's iframe you will not be able to get hold of parent window url due to cross domain issue. The solution is to write a javascript that will be hosted in content editor webpart in parent window page. It will read parent window url and pass in that as query string parameter to app's iframe.

Find sample code below. I have passed the parent url's query string in to iframe urls query string. You can modify the same to your needs.

//Get all iframes(app parts) on page.
    var iframeColl = document.getElementsByTagName('iframe');
    for (var i = 0; i < iframeColl.length; i++) {
        var thisIFrame = iframeColl[i]; // element
        var src = thisIFrame.src;
        if (src != null) {

            //need to get the query string parts  esp.find the 'redirect_uri'  and APPEND some bits to it
            var iframeUrl = src.substring(0, src.indexOf('?'));
            var vars = getUrlVars(src);
            var redirectUrl = vars["redirect_uri"];

            var decodedRedirectUrl = unescape(redirectUrl);
            var redirectParams = getUrlVars(decodedRedirectUrl);

            // get the current url  and the query string bit
            var currentPageUrl = window.location.href;
            var currentPageQueryString='';
            // if there *IS* a query string  then need to grab  and pass on to the iFRAME
            if (currentPageUrl.indexOf('?') != -1) {
                // 
                // get the query string bit for current page
                // 
                currentPageQueryString = currentPageUrl.substring(currentPageUrl.indexOf('?') + 1);
                currentPageQueryString = currentPageQueryString + "&UserID=" + userId;
                //Fix the query string
                currentPageQueryString = escape(currentPageQueryString);

                var clientID = vars["client_id"];
                //add on the extra bit (for this page querystring)
                redirectUrl = redirectUrl + "%26" + currentPageQueryString;
                //piece back together  and update the iframe
                var newUrl = iframeUrl + "?redirect_uri=" + redirectUrl + "&client_id=" + clientID;
                //update the SRC in the IFRAME
                thisIFrame.src = newUrl;
            }

        }
    } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.