sweetalert icon indicating copy to clipboard operation
sweetalert copied to clipboard

Argument error in iframes

Open polianomartini opened this issue 8 years ago • 8 comments

Hi, my dear!

I use SweetAlert in a project, but I noticed that there were some changes.

Before, I imported a JS file and a CSS into the body of HTML. I downloaded the latest "sweetalert.min.js" file and imported it as before but I'm having trouble using it.

The call below runs successfully:

swal ("Hello world!");

But this other:

swal ({
   title: "Good job!",
   text: "You clicked the button!",
   icon: "success",
});

... the following error occurs:

Uncaught SweetAlert: 1st argument ('[object Object]') is invalid

I'm doing something wrong?

polianomartini avatar Sep 15 '17 17:09 polianomartini

Hm, works for me when I run it in the JS console:

screen shot 2017-09-15 at 21 27 04

Do you have a screenshot?

t4t5 avatar Sep 15 '17 19:09 t4t5

I forgot to quote!

The import is done in a given HTML body and the access by an iframe. I'm using the expression "parent.swal", which in the old version works, but not in this new one, returning the error.

polianomartini avatar Sep 15 '17 19:09 polianomartini

Here the same problem. I use also parent.swal in an iframe which has worked in the old (1.x.x) version. :confused:

Please fix that.

edit: It happens only on extended configurations. Alerts like the following does work: parent.swal('Info!', 'Some description.', 'info');

b-vetter avatar Oct 19 '17 10:10 b-vetter

I'm also affected. I've a workaround. Define a function like this in your parent:

function myswal(value) {
     v = {}; for (var key in value) { v[key] = value[key]; };
     swal(v);
};

and call this function from the iframe.

jordeu avatar Jan 18 '18 12:01 jordeu

Any news? I noticed other users with the same problem.

More information (tested in version 2.0):

The reference (import) of the library is in the main body of the page and I'm calling the SweetAlert function on a button that is inside an iframe.

Image https://uploaddeimagens.com.br/imagens/test-png--121

This is running:

mainSystemFrame.swal ("Oops", "Something went wrong!")

However, it does not run when I use the "extended" call:

mainSystemFrame.swal ("Are you sure you want to do this?", {
    buttons: ["Oh noez!", "Aww yiss!"]
});

The error generated is:

Uncaught SweetAlert: 2nd argument ('[object Object]') is invalid

polianomartini avatar Feb 08 '18 13:02 polianomartini

@jordeu

It does not solve when using premises.

Ex:

swal("Click on either the button or outside the modal.")
.then((value) => {
  swal('The returned value is: ${value}');
});

polianomartini avatar Feb 08 '18 15:02 polianomartini

I've done some digging and think I found the cause of this bug:

When swal is called from inside an iframe, the condition prototype === Object.prototype in this line seems to return false.

On the other hand, when used outside of an iframe (or the console), prototype === Object.prototype returns true.

This results in isPlainObject returning false and an error is thrown, because swal assumes the parameters are invalid.

Apparently Object prototypes are different in different frames, @t4t5 do you know more about this?

PRs are welcome 😃

lionralfs avatar Feb 09 '18 00:02 lionralfs

Hi,

If someone still has a similar problem i've made like @jordeu but with some changes tha allows use o promises

Ex: window.mySwal = function(option){ jsonOptions = JSON.stringify(option); return swal(JSON.parse(jsonOptions)); };

zhiru avatar May 15 '19 14:05 zhiru