cancel
Showing results for 
Search instead for 
Did you mean: 

JavaScript in WD

david_fryda2
Participant
0 Kudos

Hi everyone,

I am trying to get JavaScript running with my WD application.

In the interface view, I created an Outbound plug called Exit.

I added that interface view to my Main view.

This is how I call the outbound plug :


   wdThis.wdGetMyAppInterfaceViewController().wdFirePlugExit("some JS code");

I get the following exception :

com.sap.tc.webdynpro.services.exceptions.WDRuntimeException: Cannot navigate from view MyAppInterfaceView because it is not part of the current view assembly

Can someone please help ?

Thanks.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

David,

Do not use JavaScript with WD -- this option is unsupported. Moreover, "javascript:" protocol is explicitly forbidden in Link2Url and exit plugs.

Valery Silaev

EPAM Systems

http://www.NetWeaverTeam.com

david_fryda2
Participant
0 Kudos

Hi,

If I cannot or should not use JS in WD...How can I, for example, close the main window (the window itself) using WD API ?

Thanks.

Former Member
0 Kudos

Hi,

You can create a static HTML page with the javascript for closing the window, which is


window.close();

Then fire an exit plug to this HTML page URL.

Regards,

Satyajit.

david_fryda2
Participant
0 Kudos

Hi Satyajit Chakraborty,

Can you please tell me how can I fire an exit plug to an HTML page ?

Thanks.

Note : I forwarded to a IFrame which points to an HTML file. Here is waht it contains:


<html>
<script type="text/javascript">

parentwindow = window.self;
parentwindow.opener = window.self;
//Close it
parentwindow.close();
</script>
</html>

When I call directly this URL from the internet browser it does close the window.

But if I execute the WD application that call the IFrame, it doesn't close the internet explorer.

THanks for the help.

Message was edited by: David Fryda

Former Member
0 Kudos

Hi David,

Here are the steps:

1. In your component's interface view, create an exit plug. Let's say you call it ExitPlug. While creating it make sure that it's of type "Exit".

2. Create a paramater for this exit plug. Name this parameter "Url". Note the capitalization. Make sure it is retained. This is of type string.

3. In your view add the interface view as a required controller.

4. Create your HTML file. Let's say you call it Static.html. The file you have posted is alright, but let me suggest a simpler one:


<html>
	<head>
		<script langauage="javascript">
			function closeWin(){
				window.close();
			}
		</script>
	</head>
	<body onload="javascript:closeWin();">
	</body>
</html>

5. Save this in the mimes folder for your DC under your package. You do this from the navigator view.

6. Now use the following code:


try {
String url = WDURLGenerator.getAbsoluteWebResourceURL(wdComponentAPI.getComponent().getDeployableObjectPart(),"Static.html");
wdThis.wdGet<your component name>InterfaceViewController().wdFirePlugExitPlug(url);
} catch (WDURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Regards,

Satyajit.

Message was edited by: Satyajit Chakraborty

david_fryda2
Participant
0 Kudos

Hi Satyajit,

I've done exactly what you told me...but nothing happens.

I know that the JS works fine because I run it.

It seems that when I try to run JS from WD, there is a problem.

Thanks.

Former Member
0 Kudos

Hi David,

Check these:

1. Print out the url of the HTML file thorugh


try {
String url = WDURLGenerator.getAbsoluteWebResourceURL(wdComponentAPI.getComponent().getDeployableObjectPart(),"Static.html");
wdComponentAPI.getMessageManager().reportSuccess(url);
} catch (WDURLException e) {
wdComponentAPI.getMessageManager().reportException(e.getMessage(),true);
}

Copy this URL and paste it in the address bar of the browser. Check if it runs.

2. The name of the parameter of the exit plug is "Url" and nothing else and it is of type string.

Regards,

Satyajit.

david_fryda2
Participant
0 Kudos

Hi Satyajit,

I printed out the URL and paste it into my browser and it works.

And the parameter of the exit plug is Url of type String.

Maybe my WD is configured not to run any script. Is it possible ?

Thanks.

david_fryda2
Participant
0 Kudos

Hi Satyajit,

Now it works.

The reason it didn't work previously it was because I was running it from the NWDS and not from the Portal by using the preview option.

Thanks a lot for the help.

Regards.

Former Member
0 Kudos

Hi David and Satyajit,

I have the Same problem. I followed the same way as you explained.

Created Exit Plug in Interface View with Parameter. How can i add this as Required Controller in my View. Is this with a link?

I Created a link and try to access this from View, i am not getting - FireExit Plug method?

I am not using NWDI. Please help me in solving this.

Regards,

Sai

david_fryda2
Participant
0 Kudos

Hi Sai,

As they said : "Do not use JS".

For my part, I didn't use FireExitPlug.

I created an HTML file containing JS.

I put this URL in IFrame and called it...it worked good.

I succeed closing the main window and before I displayed a message on the top bar of the IExplorer.

Regards,

Sigiswald
Contributor
0 Kudos

Hi David,

Just FYI, I also did it with an IFrame.

1. create an IFrame UI element of size 1px by 1px

2. bind its source attribute to the context

3. create the following action that's triggered each time the user presses a button:


  public void onActionPrint(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionPrint(ServerEvent)

    try {
      String printSource =
        BinaryCacheService.getInstance().getCachedWebResourceUrl(
          PRINT_SOURCE.getBytes("UTF-8"),
          WDWebResourceType.HTML);
      wdContext.currentContextElement().setIFrameSource(printSource);
    } catch (Exception e) {
      handleException(e);
    }

    //@@end
  }

with


  private static final String CLOSE = "javascript:parent.parent.close();";
  private static final String PRINT =
    "javascript:parent.parent.focus(); print();";

  private static final String CLOSE_SOURCE =
    "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""
      + " "http://www.w3.org/TR/html4/strict.dtd">rn"
      + "<html><head><title>close</title></head><body onload=""
      + CLOSE
      + ""><div>close</div></body></html>";
  private static final String PRINT_SOURCE =
    "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""
      + " "http://www.w3.org/TR/html4/strict.dtd">rn"
      + "<html><head><title>print</title></head><body onload=""
      + PRINT
      + ""><div>print</div></body></html>";

Note the "parent.parent" stuff, this may depend on how you access the WebDynpro, e.g. an iView in the EP.

Maybe not a very elegant solution, but it works...

Kind regards,

Sigiswald

FYI the BinaryCacheService utility class:


import com.sap.tc.webdynpro.services.sal.url.api.WDURLException;
import com.sap.tc.webdynpro.services.sal.url.api.WDWebResource;
import com.sap.tc.webdynpro.services.sal.url.api.WDWebResourceType;

public final class BinaryCacheService {
  private static final BinaryCacheService BINARY_CACHE_SERVICE =
    new BinaryCacheService();

  private BinaryCacheService() {
  }

  public static BinaryCacheService getInstance() {
    return BINARY_CACHE_SERVICE;
  }

  public String getCachedWebResourceUrl(byte[] data, WDWebResourceType type)
    throws WDURLException {
    if (data == null || data.length == 0) {
      return null;
    }

    return WDWebResource.getWebResource(data, type).getURL();
  }
}

Message was edited by:

Sigiswald MADOU

david_fryda2
Participant
0 Kudos

Very helpfull answer.

Thanks.

Former Member
0 Kudos

Hi,

I tried your solution with the IFrame. But I can't get it working in the Portal. I have a WD iView in NW4s and the print function works but the close function not.

If i run the WD standalone it works. I tried severale options like:

- javascript:window.top.close();

- javascript:parent.parent.focus(); close();

- etc.

Do you know if this solution works in a WD iView in the portal and what the exact statement must be?

Kind regards,

Marinus Geuze

Sigiswald
Contributor
0 Kudos

Hi Marinus,

We run the WD in an iView in the EP (2004 SPS15). The iView is a new window created by a window.open() statement in the portal. Actually I never tried to close the main portal window from within an embedded iView.

I'd suggest to try different "parent" combinations, e.g.

  • javascript:parent.close();

  • javascript:parent.parent.close();

  • javascript:parent.parent.parent.close();

  • etc.

One of them should work I suppose.

To try this out without having to build & deploy all of the time, you can get the javascript from an input field. That's a lot faster when trying things out.

BTW, in my previous post I mentioned to give the IFrame a size of 1px by 1px. Actually 0px by 0px also works fine.

Kind regards,

Sigiswald

Former Member
0 Kudos

Hi Sigiswald,

I got it working. Your suggestion with the input field was very helpful.

Kind regards,

Marinus

Former Member
0 Kudos

This is really great answer to my problem: You have no control of the response sent back from WD. So if you want to send back some data for further processing in the calling app, it's then a nightmare ( ...Exit(url) is just a partial solution -- since the parameter list length is very limited). I will try this out.

I think the whole WD design doesn't embrace this: since WD is a UI tech, it's assumed that the caller always expects some UI output instead of a bunch of data.

But the thing is that if you embed your webdynpro app into another app like SRM, basically u work inside SRM and call the webdynpro app UI only to get some data and want to stay inside the SRM --- WD just doesn't allow this option: imagine you buy something in SRM and call the WD app to select a few items, then SRM expects the data of the selected items sent back in the response --- you cannot in most cases cause u have no access to the underlying http response. Of course you can do some workaround but it almost always requires the existing App (SRM in this example) to be modified for your specific WD app (in SAP, forget it ), or something like a servlet in the middle. So the solution here is really a great thing.

I read somewhere else in this forum that even this solution might not be supported in the future, just plz don't do it. I understand the decision that you don't want to support the JS inside WD to avoid misuses (i remember JS can get really messy and human-killing). But at least leave this as a final cure for most of the projects...... Please!!!

Best regards,

Ge

Former Member
0 Kudos

Very helpful.

I had trouble finding the BinaryCacheService class, but then I found an implementation of getCachedWebResourceUrl on

Thanks.

Sigiswald
Contributor
0 Kudos

Hi Mikael,

You're absolutely right, I forgot to post the code about our BinaryCacheService utility class. I'll do that tomorrow when I have access to our DTR server. But it looks you already found a similar implementation

Thanks for this feedback!

Kind regards,

/Sigiswald

Former Member
0 Kudos

Hi All,

I do the exactly what you guys suggested, but I still got the error message:

<b>Exit-Plug must no be triggered with an URL when running in portal. Use portal navigation instead to navigate to another application!</b>

This is what I do:

1. In your component's interface view, create an exit plug. Name it as - ExitPlug.It's type as "Exit".

2. Create a paramater for this exit plug. Name this parameter "Url". It should be exactly like the same name.

3. In your view add the interface view as a required controller.

4. Create your HTML file. Let's say you call it Static.html.

<html>

<head>

<script langauage="javascript">

function closeWin(){ window.close();

}

</script>\

</head>

<body onload="javascript:closeWin();"> </body></html>

5. Save this in the mimes folder for your DC under your package. You do this from the navigator view.

6. Now use the following code:

try {String url = WDURLGenerator.getAbsoluteWebResourceURL(wdComponentAPI.getComponent().getDeployableObjectPart(),"Static.html");wdThis.wdGet<your component name>InterfaceViewController().wdFirePlugExitPlug(url);} catch (WDURLException e) {}

I am able to print out the Static.html URL string....

you metioned about IFrame....what is it???

Do I need to do that?

thanks,

Zita

Sigiswald
Contributor
0 Kudos

Hi Zita,

Apparently (I didn't test it) as of 2004s, when a WebDynpro is running inside the portal, it's no longer possible to trigger an exit plug that points to a URL. If you try running your WebDynpro standalone, I think it'll still work.

However, there's a second way to achieve what you want, executing some javascript, and that's by using an IFrame. An IFrame is actually an inline subwindow, it's a standard HTML element. Also in WebDynpro, you have an IFrame UI element: IWDIFrame. It has a mandatory "source" property that points to the URL of the page to display.

But do note that the 2004s SPS8 <a href="https://media.sdn.sap.com/javadocs/NW04s/SPS8/wd/com/sap/tc/webdynpro/clientserver/uielib/standard/api/IWDIFrame.html">IWDIFrame</a> javadocs say that "<i>Deprecated. This UI element may be withdrawn with the first new NetWeaver release in 2006 as it is no longer needed.</i>" That's too bad, because I don't see another way to use javascript. I understand it's bad practice to use javascript from within WebDynpro, but sometimes, although very rarely, I really need it. I really hope SAP leaves the IWDIFrame element as is or provides an alternative to use javascript.

Kind regards,

/Sigiswald

Former Member
0 Kudos

thank you. I have solved this problem. I do not use exit plug.

thanks anyway.

Zita

Answers (1)

Answers (1)

Former Member
0 Kudos

Simply don't use Javascript as it is not part of the programming model.

Armin