<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>梁剑的Blog &#187; 跨域</title>
	<atom:link href="http://icomes.net/tag/%e8%b7%a8%e5%9f%9f/feed/" rel="self" type="application/rss+xml" />
	<link>http://icomes.net</link>
	<description>做有趣的事，做有用的人</description>
	<lastBuildDate>Sat, 05 Nov 2011 18:23:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>遭遇JS跨域问题</title>
		<link>http://icomes.net/2009/01/23/%e9%81%ad%e9%81%87js%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98/</link>
		<comments>http://icomes.net/2009/01/23/%e9%81%ad%e9%81%87js%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 16:37:45 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[跨域]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=285</guid>
		<description><![CDATA[想在mail的一个页面里，用隐藏iframe引用soso的一个页面，加载后将数据取出。 但一直报错，说&#8230;undefine。 到群里咨询过前台高手们，原来又遇到所谓的“跨域”问题了。 处于安全性的考虑，JavaScript不允许脚本处理来自不同域(Domain)的资源。 用里的话说，就是同源策略(Same-Origin Policy)： 一个脚本只能读取与它同源（如由同一个主机下载、通过同一个端口下载或者下载协议相同）的窗口或文档的属性。 对于我的需求，常规的解决跨域问题的方法都不适用，而非常规手段又过于复杂，代价太大。 只有作罢了。 我所知的常规手段包括： 设置document.domain，只适合位于不同子域的页面中使用 采用proxy页面中转 非常规手段 借助Flash GreaseMonkey 很久没写JS了~~~ function Search&#40;&#41; &#123; var keyword = $&#40;&#34;keyword&#34;&#41;.value; debug&#40;&#34;search for &#34; + keyword&#41;; var url = &#34;http://qzone.soso.com/qz.q?&#38;amp;sc=qz&#38;amp;pid=qz.s.idx&#38;amp;ch=s.qz.diary&#38;amp;pg=1&#38;amp;ty=diary&#38;amp;w=&#34; + keyword; &#160; var soso = document.createElement&#40;&#34;iframe&#34;&#41;; soso.setAttribute&#40;&#34;onload&#34;, &#34;ParseResult()&#34;&#41;; soso.setAttribute&#40;&#34;src&#34;, url&#41;; soso.setAttribute&#40;&#34;id&#34;, &#34;soso&#34;&#41;; document.getElementsByTagName&#40;&#34;body&#34;&#41;&#91;0&#93;.appendChild&#40;soso&#41;; return false; &#125; &#160; function ParseResult&#40;&#41; &#123; debug&#40;&#34;begin to [...]<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Ficomes.net%2F2007%2F12%2F26%2F%25E5%259C%25A8flash%25E4%25B8%25AD%25E8%25B0%2583%25E7%2594%25A8js%25E5%2587%25BD%25E6%2595%25B0%25E7%259A%2584%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F01%2F23%2F%25E9%2581%25AD%25E9%2581%2587js%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">在Flash中调用JS函数的跨域问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Ficomes.net%2F2008%2F07%2F07%2Fjs%25E7%259A%2584%25E6%25B7%25B1%25E6%258B%25B7%25E8%25B4%259D%25E5%2587%25BD%25E6%2595%25B0%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F01%2F23%2F%25E9%2581%25AD%25E9%2581%2587js%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">js的深拷贝函数</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Ficomes.net%2F2007%2F10%2F14%2Fmysql%25E5%2592%258Cphp%25E7%259A%2584%25E4%25B9%25B1%25E7%25A0%2581%25E9%2597%25AE%25E9%25A2%2598%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F01%2F23%2F%25E9%2581%25AD%25E9%2581%2587js%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">mysql和php的乱码问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Ficomes.net%2F2008%2F08%2F20%2Fubuntu%25E4%25B8%258B%25E8%25A7%25A3%25E5%2586%25B3mp3%25E6%2596%2587%25E4%25BB%25B6%25E4%25B9%25B1%25E7%25A0%2581%25E9%2597%25AE%25E9%25A2%2598%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F01%2F23%2F%25E9%2581%25AD%25E9%2581%2587js%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">ubuntu下解决mp3文件乱码问题的方法</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>想在mail的一个页面里，用隐藏iframe引用soso的一个页面，加载后将数据取出。<br />
但一直报错，说&#8230;undefine。<br />
到群里咨询过前台高手们，原来又遇到所谓的“跨域”问题了。</p>
<p>处于安全性的考虑，JavaScript不允许脚本处理来自不同域(Domain)的资源。<br />
用里的话说，就是同源策略(Same-Origin Policy)：</p>
<blockquote><p>一个脚本只能读取与它同源（如由同一个主机下载、通过同一个端口下载或者下载协议相同）的窗口或文档的属性。</p></blockquote>
<p>对于我的需求，常规的解决跨域问题的方法都不适用，而非常规手段又过于复杂，代价太大。<br />
只有作罢了。</p>
<p>我所知的常规手段包括：</p>
<ol>
<li>设置document.domain，只适合位于不同子域的页面中使用</li>
<li>采用proxy页面中转</li>
</ol>
<p>非常规手段</p>
<ol>
<li>借助Flash</li>
<li>GreaseMonkey</li>
</ol>
<p>很久没写JS了~~~</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> keyword <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;keyword&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
	debug<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search for &quot;</span> <span style="color: #339933;">+</span> keyword<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://qzone.soso.com/qz.q?&amp;amp;sc=qz&amp;amp;pid=qz.s.idx&amp;amp;ch=s.qz.diary&amp;amp;pg=1&amp;amp;ty=diary&amp;amp;w=&quot;</span> <span style="color: #339933;">+</span> keyword<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> soso <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;iframe&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	soso.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onload&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;ParseResult()&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	soso.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">,</span> url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	soso.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;soso&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>soso<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> ParseResult<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	debug<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;begin to parse&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> soso <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;soso&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>soso<span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//	var ids = document.frames[0].contentWindow.document.getElementsByTagName(&quot;div&quot;);</span>
	<span style="color: #003366; font-weight: bold;">var</span> ids <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;soso&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">contentWindow</span>.<span style="color: #660066;">document</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> ids.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> ids<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> d.<span style="color: #660066;">getAttribte</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ss_c&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;qz.show.res&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			debug<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
			debug<span style="color: #009900;">&#40;</span>d.<span style="color: #660066;">className</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	debug<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;end of parse&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;soso&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Ficomes.net%2F2007%2F12%2F26%2F%25E5%259C%25A8flash%25E4%25B8%25AD%25E8%25B0%2583%25E7%2594%25A8js%25E5%2587%25BD%25E6%2595%25B0%25E7%259A%2584%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F01%2F23%2F%25E9%2581%25AD%25E9%2581%2587js%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">在Flash中调用JS函数的跨域问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Ficomes.net%2F2008%2F07%2F07%2Fjs%25E7%259A%2584%25E6%25B7%25B1%25E6%258B%25B7%25E8%25B4%259D%25E5%2587%25BD%25E6%2595%25B0%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F01%2F23%2F%25E9%2581%25AD%25E9%2581%2587js%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">js的深拷贝函数</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Ficomes.net%2F2007%2F10%2F14%2Fmysql%25E5%2592%258Cphp%25E7%259A%2584%25E4%25B9%25B1%25E7%25A0%2581%25E9%2597%25AE%25E9%25A2%2598%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F01%2F23%2F%25E9%2581%25AD%25E9%2581%2587js%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">mysql和php的乱码问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Ficomes.net%2F2008%2F08%2F20%2Fubuntu%25E4%25B8%258B%25E8%25A7%25A3%25E5%2586%25B3mp3%25E6%2596%2587%25E4%25BB%25B6%25E4%25B9%25B1%25E7%25A0%2581%25E9%2597%25AE%25E9%25A2%2598%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%2F&from=http%3A%2F%2Ficomes.net%2F2009%2F01%2F23%2F%25E9%2581%25AD%25E9%2581%2587js%25E8%25B7%25A8%25E5%259F%259F%25E9%2597%25AE%25E9%25A2%2598%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">ubuntu下解决mp3文件乱码问题的方法</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2009/01/23/%e9%81%ad%e9%81%87js%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

