如果我想從其他網站獲取內容/文本,我該怎麼辦? 例如,如果我想從網站上獲取最新的股票價格或某物。從另一個域獲取innertertext/innerHTML
是否有可能在javascript中這樣做,但從外部域拉。
var textAlr
textAlr = document.getElementById("thedivid").innerHTML
function alertText(){
window.alert(textAlr);
}
在此先感謝(:
如果我想從其他網站獲取內容/文本,我該怎麼辦? 例如,如果我想從網站上獲取最新的股票價格或某物。從另一個域獲取innertertext/innerHTML
是否有可能在javascript中這樣做,但從外部域拉。
var textAlr
textAlr = document.getElementById("thedivid").innerHTML
function alertText(){
window.alert(textAlr);
}
在此先感謝(:
這取決於其他域的設置,如果它允許從你可以做一個外部域訪問...
$.get('http://some-other-domain.com/something.html', function(html){
var text = $(html).find('#thedivid').html();
alert(text);
});
使用負載()函數可以幫助你解決這個問題:
$('#the_target_div').load("url.html #thedivid", function() {
// stuff to do when content is ready
});
這是一個棘手的事情,它通常禁止ñ。它調用了一個跨域錯誤。如果您有權採取內容,您可以看到如下設置: JavaScript permission denied. How to allow cross domain scripting between trusted domains?
否則,最好的方法是從服務器端使用cUrl。
爲什麼不試試iframe –
你可以使用API來做到這一點。 – Anup
同源政策,瞭解它。 – epascarello