6

Is there a way to detect using sp.js or jquery to detect if the current context is in a modal dialog box?

Im not sure I explained that right, so here is a situation:

We have a branded system master page due to some front facing blogs and list views so one of our requirements is to have it look exactly like the branded custom masterpage. Now, due to the styling applied to the system masterpage, the modal dialog boxes for actions like creating a new page are messed up.

Looking at the source, it looks like an iframe is used inside the modal dialog box, so its opening up a new window. My intention is to have a jquery function detect if the current page is a modal dialog box, and if it is, register a new css to override the styles to make the dialog box look normal. Is this possible?

6 Answers6

15

Try this:

if (window.location.search.match("[?&]IsDlg=1"))
Steve Lineberry
  • 7,960
  • 22
  • 24
4

When in Dialog mode the window.frameElement is NEVER null. use this as example on how to close it remotely

var parentObject = window.frameElement;
    if (parentObject != null) {
        var waitDialog = parentObject.waitDialog;
        if (waitDialog != null) {
            waitDialog.close();
        }
    }
Marius Constantinescu
  • 12,771
  • 16
  • 28
3

You could simply use javascript to identify if the page is loaded in an iframe: http://www.24hourapps.com/2009/01/check-if-page-is-loaded-in-iframe-using.html

var isInIFrame = (window.location != window.parent.location) ? true : false;

SharePoint dialog boxes are simply iframes.

John Chapman
  • 11,941
  • 6
  • 36
  • 62
3

I have added my comments to the above answers. Here are a couple additional - untested - suggestions:

  1. To build on Steve's suggestion, reload the url without IsDlg (simple, just not ideal from a performance viewpoint):

    window.location.href=window.location.href.replace(/[?&]IsDlg=1/,"");

  2. SharePoint uses the class s4-notdlg to identify what should be hidden in a modal dialog. Use css or jQuery to overwrite or remove this class.

Christophe
  • 5,301
  • 5
  • 33
  • 60
0

Here's what I use:

var currUrl = document.location.toString().toUpperCase();
var isDlg = GetUrlKeyValue('ISDLG', false, currUrl, true);
if(isDlg == 1){
    //form is in a dialog
}else{
    //not in a dialog
};
Ice Cube
  • 73
  • 8
0
// How to check if modal window is opened - 
SP.UI.ModalDialog.get_childDialog() ? console.log('modal') : console.log('no modal');
Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61