您现在的位置:首页 --> JavaScript --> 跨域请求的iframe解决方案(2)
跨域请求的iframe解决方案(2)
浏览:3103次 出处信息
将上一篇文章中的代码封装一下,基于jQuery。
用法:
以下是代码片段: // xs的意思是cross site $.xsget({ url: "http://127.0.0.1/server.html", callback: function (data) { alert(data); } }); |
源代码:
以下是代码片段: (function ($) { $.extend({ "xsget": function (options) { $.extend(options, $.xsget.defaults); var iframe = document.createElement("iframe"), same_domain = false; iframe.style.display = "none"; document.body.appendChild(iframe); // 当iframe加载完之后触发的函数 function iframe_load() { if (same_domain) { // 调用回调函数 if (typeof options.callback === "function") { options.callback(iframe.contentWindow.name); } // 关闭iframe的窗口 iframe.contentWindow.close(); // 移除iframe document.body.removeChild(iframe); } else { same_domain = true; iframe.contentWindow.location = options.proxyUrl; } } // 在IE下要用attachEvent来添加iframe的onload事件 if (iframe.attachEvent) { iframe.attachEvent("onload", function () { iframe_load(); }); } else { iframe.onload = iframe_load; } iframe.src = options.url; } }); $.extend($.xsget, { "defaults": { // 默认的空白页面,在网站的根目录下 proxyUrl: "/empty.html" } }); })(jQuery); |
建议继续学习:
- iframe大小自适应 (阅读:8614)
- iframe里src="about:blank"的问题。 (阅读:6781)
- 优雅绝妙的Javascript跨域问题解决方案 (阅读:6687)
- jQuery中getJSON跨域原理详解 (阅读:5548)
- 跨域请求的iframe解决方案(1) (阅读:5318)
- ajax-cross-domain (阅读:4926)
- BO报表系统嵌入Iframe在firefox下的错误修改 (阅读:4570)
- ie下iframe输入框焦点丢失解决方案 (阅读:4223)
- 利用跨域资源共享(CORS)实现ajax跨域调用 (阅读:4070)
- 研究ext发现ajax跨域实现 (阅读:3683)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:跨域请求的iframe解决方案(1)
后一篇:JS 常用继承实现方式 >>
文章信息
- 作者:小寒 来源: 记事本
- 标签: iframe 跨域
- 发布时间:2010-07-27 23:29:57
建议继续学习
近3天十大热文
- [305] Go Reflect 性能
- [14] [译]Google Chrome中的高性能网
- [10] Mac下.apk的反编译
- [10] 在FreeNAS/BSD搭建基于Nginx+
- [10] webapp网页调试工具Chrome Dev
- [10] 精于图片处理的10款jQuery插件
- [9] jQuery性能优化指南
- [9] 程序中的“多线程”
- [9] rsync同步的艺术
- [8] 内存表在同步环境注意事项