开发者

Populate a field with a value from a field from another page on another domain

I'm trying to create a field which would pull values from another field on another page that's located on another domain.

There are 2 fields: #div-one (on page-one) and #div-two (on page-two)

There are two pages: page-one (on domain "One.com") and page-two (on domain "Two.com")

When a user opens page-one I want #div-one to pull the value that is inside #div-two on page-two located on Two.com.

I found this code:

<script type="text/javascript">
function pullValueFromPageTwo(){
    var val = $('[#div-two]').text();
    $('#div-one').val(val);
}
</script>
开发者_JS百科

But I didn't found any clue of how to use that code to pull a value from another page and domain using jquery.


JSONP is another method for circumventing same origin policies. jQuery 1.5 has improved support for it.

http://api.jquery.com/jQuery.getJSON/
http://en.wikipedia.org/wiki/JSON#JSONP


Or you could get the data from Two.com with something else (YQL {http://developer.yahoo.com/yql/} for example) and then work with the results using javascript (as it returns XML this is very possible)..

An example query that you can put in the address bar to see the results of:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20xpath=%22/html/body/div[3]/div/div/div[2]/div/div[2]/div[2]/div[2]/div/div[3]/ul/li/h2%22%20and%20url=%22http://www.bbc.co.uk/news/%22;&format=xml&env=http://datatables.org/alltables.env

You can then get the value from Two.com on One.com with a little bit of ajax and do what you want with it.


jQuery on One.com can not access Two.com because of same origin policy.

However, if they were on the same domain, something like this should work...

var otherPage = $('body').load('/echo/html/', function(html) {

    var text = otherPage.find('#div-two').text();

    $('#div-one').html(text);

}),
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜