i followed a tutorial that created a button and got it working for what it was. it created a button and i was able to trigger the modal by clicking it. however i want this modal to load upon the page loading.
i found another post by @PaulOB and simply pasting that exact code did what i wanted it to, however it obviously wasn’t using my modal. i’m trying to incorporate what worked there into mine, but i can’t seem to figure out how to trigger this upon the page load.
the code is obviously set up to make a button and trigger the modal when clicking it, however im unsure what to alter to make it trigger on the page load
it is at the end (after all of the code in the first post)
no errors showing
before we get too invested in this, is there a way to do this without a button? i only used that as a starting point; i don’t actually want a button on the page; just for it to show upon loading. is there a way to somehow apply the code in the “another post” link in the first post to this scenario? it seems to just call $(‘name’).modal(‘show’) but that doesnt work here, nor does trying to simply call showModal(“name”) like it does inside click(). (obviously replacing “name” with the actual name)
@Mittineague that can be found here. i originally tried putting the link in the original post but i got hit with a link restriction message so i couldn’t
Well, Johani is a member of the CDCK team and he has experience working with theming so I trust that post from last October is accurate for what it does.
Modals can be a bit obtrusive unless users take active steps to initiate them, so having a modal display on a page load event isn’t something I’ve looked into doing. I don’t know of any “page load modal” tutorials but I think if you look at the code for other “on page load” themes / theme components there’s a good chance you will be able to figure out how. eg. github repo source code
@Mittineague the code in the “another post” hyperlink in the first post does load it on the page load. my question here is trying to see if i can somehow apply what was done there, to this scenario
AFAIK, custom widgets are possible, but I think it would be easier to use one that’s already there to use. Does this have to be a modal? Can you use an existing widget instead? Depending on what your use case is, maybe using category permissions or a banner would be adequate.
@Mittineague i would definitely prefer a modal. and yeah i was able to get something to pop up when the page loaded using that other custom code, but ideally i would get a modal with my theme to load. if this isnt possible, maybe we can go back to what @James_Hibbard mentioned earlier and simulating the button click when the page loads? wasnt able to get that working either
The problem with using the load event is that the widget is added after the load event so by the time the widget is there it’s listening for an event that has already happened and it’s too late.
I think the easiest - but hacky - way would be to use setTimeout instead of click.
@Mittineague@James_Hibbard this worked! setting the timeout triggers it. i wonder why it doesn’t work when i straight up try to call it?
anyway, if y’all have some extra time, i have two final issues on this topic.
my discourse site is set up so that login is required. is there a way to make this only pop up if a user is logged in, as opposed to also when they are not? currently it shows the “An account is required. Please create an account or log in to continue.” message, but the modal pops up in front of it
i don’t want this to pop up every time the user loads the site. is there a way to implement cookies here? i replaced the timeout code (that works) and put it inside a statement like this:
if (!Cookies.get("popup")) {
setTimeout(function() { showModal("myModal"); }) // the code that worked on its own
Cookies.set('popup', 'popped');
}
It’s most likely a timing issue. Calling setTimeout without a delay will run whatever callback function you pass it when the current stack has been cleared. This gives the code that the callback function relies on (the implementation of the showModal method here) chance to run first and be available.
This might be a little trickier. When a user is logged in, Discourse creates a user_signed_in cookie. You could try checking for the presence of that before displaying the modal. Not sure how well that’ll work, though.
That’s easier. This article is a little old, but demonstrates the general principle:
@James_Hibbard i’m having trouble getting these to work… i’m trying to use js-cookie and seems that it isn’t working. i’m doing a basic scenario first before applying it to my situation. am i missing something here? there is no alert coming up at all
<script src="https://cdn.jsdelivr.net/npm/js-cookie@beta/dist/js.cookie.min.js">
if (!Cookies.get("popup")) {
alert("1"); // first visit
Cookies.set("popup", "true");
} else {
alert("2"); // not first visit
}
</script>