想在mail的一个页面里,用隐藏iframe引用soso的一个页面,加载后将数据取出。
但一直报错,说…undefine。
到群里咨询过前台高手们,原来又遇到所谓的“跨域”问题了。
处于安全性的考虑,JavaScript不允许脚本处理来自不同域(Domain)的资源。
用里的话说,就是同源策略(Same-Origin Policy):
一个脚本只能读取与它同源(如由同一个主机下载、通过同一个端口下载或者下载协议相同)的窗口或文档的属性。
对于我的需求,常规的解决跨域问题的方法都不适用,而非常规手段又过于复杂,代价太大。
只有作罢了。
我所知的常规手段包括:
- 设置document.domain,只适合位于不同子域的页面中使用
- 采用proxy页面中转
非常规手段
- 借助Flash
- GreaseMonkey
很久没写JS了~~~
function Search() { var keyword = $("keyword").value; debug("search for " + keyword); var url = "http://qzone.soso.com/qz.q?&sc=qz&pid=qz.s.idx&ch=s.qz.diary&pg=1&ty=diary&w=" + keyword; var soso = document.createElement("iframe"); soso.setAttribute("onload", "ParseResult()"); soso.setAttribute("src", url); soso.setAttribute("id", "soso"); document.getElementsByTagName("body")[0].appendChild(soso); return false; } function ParseResult() { debug("begin to parse"); var soso = $("soso"); if(!soso) return; // var ids = document.frames[0].contentWindow.document.getElementsByTagName("div"); var ids = document.getElementById("soso").contentWindow.document.getElementsByTagName("div"); for( var i = 0; i < ids.length; i++) { var d = ids[i]; if( d.getAttribte("ss_c") == "qz.show.res") { debug("found"); } else { debug(d.className); } } debug("end of parse"); document.getElementsByTagName("body")[0].removeChild($("soso")); }
近期评论