Scriptrunner fragment displays modal as entire page
Asked by Jaden Zerbe:

Hello,

I am attempting to create a button that opens a modal within the confluence UI using scriptrunner. I have created a web fragment which seems correct from the documentation. I have attached a screenshot of my configuration. I have linked my fragment to a REST endpoint. The REST endpoint is directly copied from <https://scriptrunner.adaptavist.com/latest/jira/fragments/WebItem.html> under the Dialogs (Advanced) section.

After doing all of this, my modal appears alone with no styling in its own tab/page when my link is clicked. I am confused as to why it is not appearing as a modal on the same screen as the clicked button. Any help is appreciated.

Thank![WebItem.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/76944i5A24F4696FB16C3E/image-size/large?v=v2&px=999 "WebItem.PNG")


Answers:
author: Peter-Dave Sheehan
body: That might be a bug/design flaw in scriptrunner. Try to put the same webitem on a page menu instead.

For this web fragment config to work, scriptrunner has to make sure they deploy some javascript in a given context that will listen to this link-click event and create a dialog instead of using the default behavior of the link (which is to navigate tot eh url).

If you can't get them to fix it or don't want to wait, then you have to deploy your own javascript in that specific context and change the "Do What" dropdown to "Do Nothing I will write my own javascript".

Something like this has worked for me in a different context:

```
(function ($) {
   $(function () {

      $('body').off('click', '#your-web-item-key').on('click', '#your-web-item-key', function(event){
            event.preventDefault();
            var ancestorAnchor = $(this).closest("[href]");
            AJS.$.ajax({
                type: "GET",
                dataType: "html",
                url: ancestorAnchor.attr("href")
            }).done(function(data) {
                AJS.dialog2($(data)).show();
            }).fail(function(jqXHR, textStatus, errorThrown) {
                console.warn("Failed to execute remote:", errorThrown);
            });
        });

   });
})(AJS.$);
```

This will bind the click event of your web item so that when you click it, it will prevent the navigation (preventDefault) then fetch the content of your rest API using ajax by reading the href attribute and then display an AJS.dialog2 with the data retrieved from the rest api.

This page may be helpful: <https://docs.atlassian.com/aui/8.6.0/docs/dialog2.html>

comments: author: Michael Bramhall
body: I have ran into the same problem as the poster. I have a fragment that pops up a UI box within the current context working in Jira, but when i try to do the same implementation in confluence it redirects to its own page, and not display in the current context. Looks like a bug and considering the original post here is 2019 does not look like they will fix it any time soon.

Regarding the Javascript above I have never used it - where do i put the code, is it still a rest end point?


Tags: adaptavist

addon-com.onresolve.confluence.groovy.groovyrunner

confluence-server

scriptrunner

server