Trying to load an API and a JS file dynamically

后端 未结 3 565
别跟我提以往
别跟我提以往 2021-02-09 02:46

I am trying to load Skyscanner API dynamically but it doesn\'t seem to work. I tried every possible way I could think of and all it happens the content disappears.

I tri

3条回答
  •  一生所求
    2021-02-09 02:50

    I belive what you want is it:

    function loadSkyscanner()
    {
        function loaded()
        {
            t.skyscanner.load('snippets', '1', {'nocss' : true});
    
            var snippet = new t.skyscanner.snippets.SearchPanelControl();
            snippet.setCurrency('GBP');
            snippet.setDeparture('uk');
            snippet.draw(document.getElementById('snippet_searchpanel'));
        }
    
        var t = document.getElementById('sky_loader').contentWindow;
        var head = t.document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.onreadystatechange= function() {
            if(this.readyState == 'complete') loaded();
        }
        script.onload= loaded;
        script.src= 'http://api.skyscanner.net/api.ashx?key=PUT_HERE_YOUR_SKYSCANNER_API_KEY';
        head.appendChild(script);
    }
    
    $("button").click(function(e)
    {
        loadSkyscanner();
    });
    

    It's load skyscanner in iframe#sky_loader, after call loaded function to create the SearchPanelControl. But in the end, snippet draws in the main document. It's really a bizarre workaround, but it works.

    The only restriction is, you need a iframe. But you can hide it using display:none.

    A working example

    EDIT

    Sorry guy, I didn't see it. Now we can see how awful is skyscanner API. It puts two divs to make the autocomplete, but not relative to the element you call to draw, but the document. When a script is loaded in a iframe, document is the iframe document.

    There is a solution, but I don't recommend, is really a workaround:

        function loadSkyscanner()
    {
        var t;
        this.skyscanner;
        var iframe = $("");
    
        function realWorkaround()
        {
            var tbody = t.document.getElementsByTagName("body")[0];
            var body = document.getElementsByTagName("body")[0];
    
            while( tbody.children.length != 0 )
            {
                var temp = tbody.children[0];
                tbody.removeChild( temp );
                body.appendChild( temp );
            }
        }
    
        function snippetLoaded()
        {
            skyscanner = t.skyscanner;
    
            var snippet = new skyscanner.snippets.SearchPanelControl();
            snippet.setCurrency('GBP');
            snippet.setDeparture('uk');
            snippet.draw(document.getElementById('snippet_searchpanel'));
    
            setTimeout( realWorkaround, 2000 );
        }
    
        var loaded = function()
        {
            console.log( "loaded" );
            t = document.getElementById('sky_loader').contentWindow;
    
            t.onLoadSnippets( snippetLoaded );
        }
    
        $("body").append(iframe);
        iframe.load(loaded);
    }
    
    $("button").click(function(e)
    {
        loadSkyscanner();
    });
    

    Load a iframe with another html who loads and callback when the snippet is loaded. After loaded create the snippet where you want and after set a timeout because we can't know when the SearchPanelControl is loaded. This realWorkaround move the autocomplete divs to the main document.

    You can see a work example here

    The iframe loaded is this

    EDIT

    Fixed the bug you found and updated the link.

    the for loop has gone and added a while, works better now.

        while( tbody.children.length != 0 )
        {
            var temp = tbody.children[0];
            tbody.removeChild( temp );
            body.appendChild( temp );
        }
    

提交回复
热议问题