`
ayuayufan
  • 浏览: 28759 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

iframe跨域引用其他url造成拒绝访问的解决办法

阅读更多

有这样一段代码,需要在隐藏的iframe中调用其他的url:

function a(){
	var url=...;//跨域url
	var param=...;
	iframe3.document.location=encodeURI(url+param);
}
 

 

这个函数在同一页面上调用一次,没问题。再点一次呢?问题出现:JS提示拒绝访问。

如何能够避免这个问题呢,请看下面的代码。

function b(){
	var url=...;//跨域url
	var param=...;
	var ifr=document.createElement("iframe");//以下创建一个隐藏的iframe
	ifr.setAttribute("width",0);
	ifr.setAttribute("height",0);
	document.body.appendChild(ifr);
	ifr.src=encodeURI(url+param);
}
 

每次在新开的iframe中完成url所需的工作,这样就可以满足要求了。

 

注意,这样做适用于你的url仅做一些原子类的操作,如打印等。如果这个url具有连续性(比如要监听一个事件),建议你不要这样做,以免在同一个页面产生多个此类的操作。

分享到:
评论
1 楼 hzwr2008 2011-04-22  
你好!我是个新手,我想请教下一个问题,就是我现在也是遇到一个跨域的问题,但是我的iframe提交我用了httprequest.js的 httpIframeSubmit(form,callback,null)函数,url包含在form里,js里的代码如下, this.iframeSend = function(uri, formRef) {
   //alert(document.documentElement.innerHTML);
// Routes a request through our hidden IFRAME. Pass a URI, and optionally a
// reference to a submitting form. Requires proprietary 'readyState' property.
if (!document.readyState) return false;

// Opera fix: force the frame to render before setting it as a target.
if (document && document.getElementById && document.getElementById(this.iframeID))
  var o = document.getElementById(this.iframeID).offsetWidth;
// Either route the form submission to the IFRAME (easy!)...
 
if (formRef){
      formRef.setAttribute('target', this.iframeID);
      formRef.submit();
}else{
  // ...or load the provided URI in the IFRAME, checking for browser bugs:
  // 1) Safari only works well using 'document.location'.
  // 2) Opera needs the 'src' explicitly set!
  // 3) Konqueror 3.1 seems to think ifrDoc's location = window.location, so watch that too.
  var ifrDoc = this.iframe.contentDocument || this.iframe.document;
  if (!window.opera && ifrDoc.location &&
   ifrDoc.location.href != location.href) ifrDoc.location.replace(uri);
  else this.iframe.src = uri;
}

// Either way, set the loading flag and start the readyState checking loop.
// Opera likes a longer initial timeout with multiple frames running...
var reqobject = this;
this.loadingURI = uri;
setTimeout(iframeCheck,(window.opera ? 250 : 100)); 
function iframeCheck() {
// Called after a timeout, periodically calls itself until the load is complete.
// Get a reference to the loaded document, using either W3 contentDocument or IE's DOM.
//doc = reqobject.iframe.contentDocument || reqobject.iframe.document;
doc = reqobject.iframe.contentDocument || window.frames[reqobject.iframe.name].document;

// Check the IFRAME's .readyState property and callback() if load complete.
// IE4 only seems to advance to 'interactive' so let it proceed from there.
var il = reqobject.iframe.location, dr = doc.readyState;

if ((il && il.href ? il.href.match(this.loadingURI) : 1) &&
(dr == 'complete' || (!document.getElementById && dr == 'interactive'))){

     if (reqobject.callback) reqobject.callback(doc,this.loadingURI);
     this.loadingURI ='';
}
else setTimeout(iframeCheck , 50);
}

return true;
}
,我该如何修改啊?

相关推荐

Global site tag (gtag.js) - Google Analytics