2

From a form in a web page, I'm opening a stored process (STP) in a new window.

<form target="_blank" action="http://<UrlToMySTP>" method="get" enctype="multipart/form-data">

that STP does someting and when it's done I need to close it's window. So I tried to close it with javascript like this

data _null_;
    file _webout;
    put '<HTML>';
    put '   <HEAD>';
    put '   </HEAD>';
    put '   <BODY onLoad="window.close()">';
    put '   </BODY>';
    put '</HTML>';
    run;

but I get an error message that says:

Scripts may close only the windows that were opened by it

I cannot find any solution for this. Is is possible to circumvent the problem to get to the same result ?

stallingOne
  • 3,633
  • 3
  • 41
  • 63
  • Have you tried testing it with the same javascript that is independent of the STP? – Stu Sztukowski Aug 24 '17 at 13:25
  • @StuSztukowski I'm not sure to understand what you mean. (I did try to type `window.close()` in the console of the page when it's open but that gives the same error.) – stallingOne Aug 24 '17 at 13:34
  • If you use `window.open()` to open a pop up window, you get back a reference to the opened window. Using that reference, you can close the pop up window. This slightly different question/answer shows how: https://stackoverflow.com/a/10702428/398606 – Sunil D. Aug 24 '17 at 14:56
  • @SunilD. the problem with that is that I don't know when the popup will be done doing it's job. Defining a timeout could either be too long or too short. – stallingOne Aug 24 '17 at 15:05
  • @stallingone with that reference to the pop up window, you are able to access properties and methods of the pop up window (see the "Return value" section: https://developer.mozilla.org/en-US/docs/Web/API/Window/open). So for example, you could set a property in the pop up window when it's time to close, and then use `setTimeout` to poll the pop up from the main window and wait for that property to have the desired value. Not super elegant, but... – Sunil D. Aug 24 '17 at 18:15

1 Answers1

1

You can try ajax query with dataType: "jsonp". Its can overcome CORS. STP must return json response, wrapped by name of js function, that wiil be executed in browser after ajax response.

Example

json_test.sas

 data _null_;
 file _WEBOUT;
 put "JSFunctionToAlertHTML({""Code"":0});";
 run;

html_test.html

Must contain:

$.ajax({
          url: "http://servername:port/SASStoredProcess/do?_program=path%2Fjson_test&callback=?",
          dataType: "jsonp"
        });

Make your attention to additional param callback=in url .

And

  function JSFunctionToAlertHTML(json){
    //Your code
  }

You also if you use EG to cretae stored process, you must exclude Stored Process macros from STP settings and set result capabilities to Stream.

Sanek Zhitnik
  • 716
  • 1
  • 10
  • 25
  • 1
    Lets start a discussion about this approach. When you set negative points without argument, you do not benefit society SO – Sanek Zhitnik Aug 25 '17 at 08:16
  • Interesting! Can you clarify your point though about "exclude Stored Process macros from STP settings" ? Not sure how this makes a difference.. – Allan Bowe Aug 25 '17 at 08:26
  • Hello @AllanBowe ! Its necessary to avoid the next error `ERROR: File is in use, _WEBOUT .` . If its possibly do by another way, I would like to listen)) The screen of the settings is [here](http://imgur.com/lAqww7A) – Sanek Zhitnik Aug 25 '17 at 08:39
  • aaah - I understand, it prevents the `%stpbegin` / `%stpend` macros from running. Which yes, prevents the `_webout` fileref from being used. This must be an EG option. I normally create my STPs programmatically! – Allan Bowe Aug 25 '17 at 08:41
  • @AllanBowe aw. I forgot that there is no this option in DI or MC. I will add it to my answer. Thank You! – Sanek Zhitnik Aug 25 '17 at 08:43
  • `data _null_; file _WEBOUT; put "JSFunctionToAlertHTML({""Code"":0});"; run;` is that javascript supposed to be executed ? (if so doesn't it need to be in ` – stallingOne Sep 20 '17 at 09:58
  • @stallingone hello. This code will return the jsonp response. It must contain function, that you need to write in you code, and json-string as parameter of the function. Normal json ajax queries mostly can't get throw CORS policy. – Sanek Zhitnik Sep 20 '17 at 13:34