I believe you want:
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(); });
Loads skyscanner into iframe # sky_loader, after calling the load function to create SearchPanelControl. But, in the end, the fragment is drawn in the main document. This is a really weird workaround, but it works.
The only limitation is you need an iframe. But you can hide it with display:none .
Working example
EDIT
Sorry, man, I have not seen this. Now we see how terrible the skyscanner API is. It places two divs for autocomplete, but does not apply to the element that you call to draw, but to the document. When a script is loaded in an iframe, the document is an iframe document.
There is a solution, but I do not recommend, this is really a workaround:
function loadSkyscanner() { var t; this.skyscanner; var iframe = $("<iframe id=\"sky_loader\" src=\"http://fiddle.jshell.net/orlleite/2TqDu/6/show/\"></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 the iframe with another html that loads and calls the callback when the fragment loads. After loading, create the fragment you want and after setting the timeout, because we cannot know when SearchPanelControl is loading. This realWorkaround moves the autocomplete div into the main document.
Here you can see an example of work
Loaded iframe
EDIT
The detected error was fixed and the link was updated.
the for loop went through and added some time, now it works better.
while( tbody.children.length != 0 ) { var temp = tbody.children[0]; tbody.removeChild( temp ); body.appendChild( temp ); }