2

we have the following situation:

in default.aspx we have a link:

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

and the JS code:

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);});
}

The callback can first be an url address or 2nd html code that must be executed. Option 1 fits, but in option 2, a new window will be opened where the code has been written but not executed.

It's clear, since it happens in the stream, it can't be executed. So the question, how can you fix it? Maybe a refresh(), or similar?

Because of the requirement of the customer, the workflow can not be changed, so it must be solved within doPost().

EDIT

The response in case 2 is HTML like this. This part should be executed:

<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>

Please help. Thanks.

  • What is Option 1 and Option 2. If you are trying to open a new html page which needs to be rendered in whole. You should not write data on that page. infact you should only pass html link to win.document and that should just open the link in new window and everything will be executed as normal. Something you do with dynamic url generation from one page to another. – Farrukh Subhani Feb 02 '13 at 14:18

1 Answers1

0

In your JS code it should be something like this:

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);});
}

The overall logic is this

  1. You do your ajax call on doPost
  2. Find out if data returned is of type url or anything that need to open in new window
  3. If it is url type it would have a url (check if this is not null or empty or even a valid url) then open a new window with that url. Have a read of W3C window.open for parameters
  4. If you want to open and close it for some reason just do that by keeping the window handle but you can do this on dom ready event of that new window otherwise you might end up closing it before its dom is completely loaded. (someone else might have better way)
  5. If its not url type then you do your usual stuff on this page.

If this does not make sense lets discuss.

Farrukh Subhani
  • 2,018
  • 1
  • 17
  • 25
  • Sorry, but I don't understand your code. // No need to write data to new window when its all html to be rendered by browser. -> This part is essential, because it contains the html code with Jquery scripts etc... – user1968201 Feb 02 '13 at 16:34
  • if your function returns html like a page then just call the url in new window. You do not need to get all html and then inject it into new window via this function. ajax should only return url and when you pass this url to window.open it will open it in new window. – Farrukh Subhani Feb 02 '13 at 21:00
  • the callback can return dynamic html, which can not be predicted. In this case, we don't know the url, so we can not just open in a new window. – user1968201 Feb 03 '13 at 11:15
  • What is dynamic html? Call back means you request the server and it sends html back thats it. You only need to have parameters passed in first request which can generate a dynamic url to obtain those parameters and resulting html. – Farrukh Subhani Feb 03 '13 at 15:39
  • Please look into my first post. With dynamic html I mean the bottom example: "The response in case 2...". We can not influence the response that comes from the HttpHandler. The answer is HTML code with JavaScript and a FORM, which is also making ​​a post on the next page. In short, the html must be execute. Nevertheless, thank you for your constructive ideas. – user1968201 Feb 03 '13 at 16:54
  • Ok then why dont you first create a hidden iframe and put all your form data as input fields in there and post it to new window. Have a look at this http://taswar.zeytinsoft.com/2010/07/08/javascript-http-post-data-to-new-window-or-pop-up/. All i am saying is rather than posting some data and getting results and then populating new window why cant you just populate new window first and submit data from there. Might have gone confusing by now but hope it helps. – Farrukh Subhani Feb 03 '13 at 17:31
  • http://stackoverflow.com/questions/5684303/javascript-window-open-pass-values-using-post Another answer that might be useful. – Farrukh Subhani Feb 03 '13 at 17:33