2012-08-09 37 views
0

我有動態形式在我的網址。我想要寫一個JavaScript,以便當用戶粘貼在他們的網頁的JavaScript表單應顯示。javascript顯示錶單從遠程url

<script type="text/javascript"> 
window.location.href = "http://myserver/2a249ccf59e272abb8e7b8582560a45c" 
</script> 

這個問題可以用上面的代碼中,URL redirects.I要避免重定向 心中已經嘗試了內部框架,但我不希望使用他們按要求。

+0

你的意思是說你要轉義的html,所以它不會呈現和執行,但只能在頁面上直觀顯示? – 2012-08-09 10:10:47

+0

不完全......網址內容中有動態表單。如果您將URL粘貼到瀏覽器中,表單將加載並顯示。 我想用JavaScript來做同樣的事情。基本上我想要使用JavaScript獲取網址內容。 – user1587161 2012-08-09 10:12:48

+0

好的,所以這是一個關於AJAX的問題 - 以及如何將HTML注入頁面。 – 2012-08-09 10:14:56

回答

0

您需要使用AJAX來獲取遠程頁面。您必須從與您的網頁交付相同的服務器上獲取該頁面,以避免出現相同的原始策略問題。

我會推薦使用庫來處理AJAX請求。 jQuery內置了AJAX函數,但你也可以使用其他的。使用jQuery ...

// be sure to include jQuery library in the head of your document... 

// general wrapper to execute code only after all page elements have loaded 
$(function(){ 
    $.get("http://myserver/2a249ccf59e272abb8e7b8582560a45c",function(data,status){ 
     // put the fetched data (your html form) at the end of the existing body content 
     // you could put it somewhere else by changing the `body` selector 
     $("body").append(data) 
    }) 
})