#1
  1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221

    Redirect Iframe's parent with window.top.location.href not working


    Hi;

    How come redirect iframe parent doesn't work?

    window.top.location.href

    Direct link: The HTML5 Herald

    Fiddle: https://jsfiddle.net/kv51uf7p/

    Redirect code is generated in https://timerz.xyz/timer/bc.php

    Thanks
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,904
    Rep Power
    9646
    Your AJAX returned some nice HTML. But that's all it did. Return it.

    Don't return Javascript to execute. Return a JSON response. That response, probably an object, can then contain particular data that the calling code can recognize as containing an instruction to redirect to wherever.
    Last edited by requinix; July 12th, 2018 at 04:26 AM.
  4. #3
  5. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221
    Originally Posted by requinix
    Your AJAX returned some nice HTML. But that's all it did. Return it.

    Don't return Javascript to execute. Return a JSON response. That response, probably an object, can then contain particular data that the calling code can recognize as containing an instruction to redirect to wherever.
    Hello;

    I can't explain why it wasn't working before and now it is!

    What you sau I am sure is right (most of the times) but can you explain what happened now! I changed the js from

    Code:
    (function(window) 
        {
            var currentScript = document.currentScript;
            var apiUrl = currentScript.src;
            // this is the url of the timer
            var url = createUrl(currentScript, apiUrl);
            // get the timer
            getTimer(currentScript, url);
        }
    (this));
    
    function createUrl(currentScript, apiUrl) 
        {
            // create URL parameters object
            const paramsObj = 
                {
                    action: 'load-template',
                    launch_owner_email_hashed: currentScript.getAttribute('data-launch_owner_email_hashed'),
                    launch_id: currentScript.getAttribute('data-launch_id')
                }
        // convert object to an array
        var paramsArr = Object.keys(paramsObj);
        // add '&' character between each array item
        var params = paramsArr.map(function(i) 
            {
                return encodeURIComponent(i) + '=' + encodeURIComponent(paramsObj[i])
            })
            .join('&');
    
        // add teh url path and query parameters together
        var url = apiUrl + '?' + params;
    
        return url;
    }
    
    function getTimer(currentScript, url) {
        var request = new XMLHttpRequest();
    
        request.open('GET', url, true);
    
        request.onload = function() {
            if (request.status >= 200 && request.status < 400) {
                let alert = "Success!"
                
    
                var response = request.responseText;
    
                // add the timer after the current scripting code which called it
                currentScript.insertAdjacentHTML('afterend', response);
    
                // remove the scripting code so only the timer remains
                currentScript.remove();
            } else {
                let alert = "We reached our target server, but it returned an error.";
                
            }
        };
    
        request.onerror = function() {
            let alert = "There was a connection error of some sort.";
            
        };
    
        request.send();
    }
    to

    Code:
    (function(window)
        { 
             
                var currentScript = document.currentScript; 
                var apiUrl = currentScript.src; 
    
                if (!('jQuery' in window))
                    { 
                        loadJQuery(initialize); 
                    }
                else 
                    { 
                        initialize(); 
                    } 
                function loadJQuery(cb)
                    { 
                        var script = document.createElement('script'); 
                        script.src = 'https://code.jquery.com/jquery-3.3.1.min.js'; 
                        script.type = 'text/javascript'; 
                        script.addEventListener('load', cb); 
                        document.getElementsByTagName('head')[0].appendChild(script); 
                    } 
                function initialize()
                    { 
                        //Validate 
                        var $currentScript = $(currentScript); 
                        var params = $.param({ 
                            action: 'load-template' 
                            , launch_owner_email_hashed: $currentScript.data('launch_owner_email_hashed') 
                            , launch_id: $currentScript.data('launch_id') 
                        }); 
    
                        $.get(apiUrl, params).then(function(html){ 
                            var div = $('<div style="margin:0; padding: 0;">').html(html); 
                            $currentScript.after(div); 
                        });
    
                        $(document).ready(function() 
                            {
                                console.log(1);
                            }); 
                    }
    
    
        }
    (this));
    Last edited by English Breakfast Tea; July 12th, 2018 at 08:29 PM.

IMN logo majestic logo threadwatch logo seochat tools logo