t4t5 / sweetalert

A beautiful replacement for JavaScript's "alert"
https://sweetalert.js.org
MIT License
22.4k stars 2.84k forks source link

Argument error in iframes #722

Open polianomartini opened 6 years ago

polianomartini commented 6 years ago

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?

t4t5 commented 6 years ago

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?

polianomartini commented 6 years ago

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.

b-vetter commented 6 years ago

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');

jordeu commented 6 years ago

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.

polianomartini commented 6 years ago

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 commented 6 years ago

@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}');
});
lionralfs commented 6 years ago

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 😃

zhiru commented 5 years ago

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)); };