2

次のような状況があります。

default.aspxリンクがあります:

<a href="javascript:doPost()">test</a>.

およびJSコード:

function doPost() {
    $.post('AnHttpHandlerPage.aspx',"{some_data:...}", function(data) {
        if(data.indexOf("http://")==0)
            window.open(data);
        else{
            var win=window.open();
            with(win.document) {
                open();
                write(data); //-> how to execute this HTML code? The code also includes references to other js files.
                close();
            }  
        }
    }).error(function(msg){document.write(msg.responseText);});
}

コールバックは、最初に実行する必要のあるURLアドレスまたは2番目のhtmlコードにすることができます。オプション1は適合しますが、オプション2では、コードが記述されているが実行されていない新しいウィンドウが開きます。

ストリーム内で発生するため、実行できないことは明らかです。それで、質問、どうすればそれを修正できますか?たぶん、、refresh()または同様のもの?

お客様の要件により、ワークフローを変更することはできません。そのため、内で解決する必要がありますdoPost()

編集

ケース2の応答はこのようなHTMLです。この部分は実行する必要があります

<HTML><HEAD>
<SCRIPT type=text/javascript src="http://code.jquery.com/jquery-latest.js">
</SCRIPT>
<SCRIPT type=text/javascript>                      
$(document).ready(function() {
do_something... 
});                          
</SCRIPT>
</HEAD>
<BODY>
<FORM>...</FORM>
</BODY>
</HTML>

助けてください。ありがとう。

4

1 に答える 1

0

JS コードでは、次のようになります。

function doPost() {
    $.post('AnHttpHandlerPage.aspx',"{some_data:...}", function(data) {
        //if(data.indexOf("http://")==0)
              if (data.type!="url")   //i will add a data type to my returned json so i can differentiate if its url or html to show on page.
            window.open(); // I dont know why this is there. You should
        else{
            var win=window.open(data.url); //This data.url should spit out the whole page you want in new window. If its external it would be fine. if its internal maybe you can have an Action on one of your controllers that spit it with head body js css etc.
        /*  with(win.document) {
                open();
                write(data); //-> how to execute this HTML code? The code also includes references to other js files.
                close(); */ // No need to write data to new window when its all html to be rendered by browser. Why is this a requirement.
            }  
        }
    }).error(function(msg){document.write(msg.responseText);});
}

全体的なロジックはこれです

  1. doPostでajax呼び出しを行います
  2. 返されたデータのタイプが url か、新しいウィンドウで開く必要があるものかを調べます
  3. URL タイプの場合は、URL を持ち (これが null または空でないか、有効な URL であるかどうかを確認してください)、その URL で新しいウィンドウを開きます。パラメータについては、 W3C window.openを読んでください。
  4. なんらかの理由でそれを開閉したい場合は、ウィンドウハンドルを保持することでそれを行いますが、その新しいウィンドウの dom ready イベントでこれを行うことができます。そうしないと、dom が完全に読み込まれる前に閉じてしまう可能性があります。(他の誰かがより良い方法を持っているかもしれません)
  5. URL タイプでない場合は、このページで通常の操作を行います。

これが意味をなさない場合は、議論しましょう。

于 2013-02-02T14:31:45.770 に答える