Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument error in iframes #722

Open
polianomartini opened this issue Sep 15, 2017 · 8 comments
Open

Argument error in iframes #722

polianomartini opened this issue Sep 15, 2017 · 8 comments

Comments

@polianomartini
Copy link

polianomartini commented Sep 15, 2017

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
Copy link
Owner

t4t5 commented Sep 15, 2017

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
Copy link
Author

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.

@t4t5 t4t5 changed the title Argument error Argument error in iFrames Sep 24, 2017
@t4t5 t4t5 changed the title Argument error in iFrames Argument error in iframes Sep 24, 2017
@b-vetter
Copy link

b-vetter commented Oct 19, 2017

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

Please fix that.

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

@jordeu
Copy link

jordeu commented Jan 18, 2018

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
Copy link
Author

polianomartini commented Feb 8, 2018

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
Copy link
Author

polianomartini commented Feb 8, 2018

@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
Copy link
Collaborator

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
Copy link

zhiru commented May 15, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants