文档准备在新窗口,跨域

时间:2012-10-04 09:59:44

标签: javascript jquery cross-domain

如何在文档重新加载后检查新窗口中的文档是否准备就绪。

以下是我的例子:

我需要从某个站点(它的跨域)获取搜索结果页面到新窗口。我需要先发出POST请求(他们可能会在会话中存储搜索参数),然后转到reslut页面。

这是我的代码:

var windowname = "window"+(new Date().getTime()); // so I can open multiple windows, not very relevant 
var new_window = window.open("", windowname); // prepare named window

// prepare form with post data targeted to new window
var dataform = $("<form method='post' target='"+windowname+"' action='https://www.ebi.ac.uk/chembldb/compound/smiles/'><input name='smiles' value='"+$("#id_smiles").text()+"'></form>");

// Need to get the form into page for Mozilla, webkit allows to submit forms that are not in page
$("body").append(dataform);

// submit the form and remove it, no need to keep it
dataform.submit().remove();

// form opens in my new window
$(new_window.document).ready(function(){

    // this is carried out apparently too soon, because the POST data didn't work
    // when I use timeout (commented below, but i don't like this solution) it works
    window.open("https://www.ebi.ac.uk/chembldb/index.php/compound/results/1/chemblid/asc/tab/smiles", windowname);
    // setTimeout( function(){window.open("https://www.ebi.ac.uk/chembldb/index.php/compound/results/1/chemblid/asc/tab/smiles", windowname)}, 1000);
});

在该网站上首先使用AJAX发出POST请求,然后他们就是这样,但由于它是跨域的,所以对我来说是不可能的。

2 个答案:

答案 0 :(得分:0)

我相信这是不可能的。如果您使用new_window(跨域)的引用,甚至某些浏览器会抛出异常。  我有以下异常。尝试使用网址http://www.google.com(浏览器Chrome)访问新窗口的引用时。并且引用没有属性。

不安全的JavaScript尝试从包含网址http://www.google.co.in/的网址访问包含网址Document ready in new window, cross-domain的框架。域,协议和端口必须匹配。

答案 1 :(得分:0)

您可以运行跨域的javascript代码,因为您应该使用JSONP概念(http://en.wikipedia.org/wiki/JSONP)/跨源资源共享(http://en.wikipedia.org/wiki/Cross-origin_resource_sharing)。

只需在apache服务器设置中进行一些更改。