Hello there,I need a modal window which pop-up after a time delay.
Following are my requirements:
Check for cookies :
a)if cookies are present:
display a login page
b) if cookies are not there:
display a signup page
2)when a visitor reaches end of document- a modal window should pop-up.
I’m pretty new to js and don’t know much about it,help will be highly appreciated
Thanks
First of all thanks @TechnoBear for your prompt reply.
I have developed complete logic for it but in coding area I’m just able to delay the modal popup.
It’s not my area of expertise, but I’m sure if you post the code you have so far, and explain what help you need with it, somebody else will be able to assist.
When you post code on the forums, you need to format it so it will display correctly.
You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.
@m3g4p0p I have to display modal as follow:
Check for cookies:
If cookies present:
display a modal which takes user to login page
else:
display a modal taking user to signup page.
Also I intend to show a modal when a user reaches the end of the web page.
Now I’m pretty new to JavaScript and web development,so I’m not getting how to move forward with the above requirements.
Okay then, one at a time. :-) What does cookie look like, and which is the value you’re interested in? If you’re not sure, just open the console and enter document.cookie.
hey! I’m looking for any cookie value which will help me to find out whether the user has visited for the first time on website or not. document.cookie currently displays " ".
Although it would make sense to check for a specific value in case you want to store other values in the cookie as well at some point. Suppose you’re setting the cookie in your backend with PHP like
setcookie('visited', 'true');
Then you could check if the cookie contains the string visited=true like
// The indexOf() method returns -1 if the needle is not
// found in the haystack, or the index otherwise
var visited = document.cookie.indexOf('visited=true') > -1
if (visited) {/* ... */}
More specific value checks are a bit more complicated; as explained in the MDN article above, the cookie is just a semicolon-separated string of key/value pairs, which has to be parsed first. There are quite a few small libraries out there to facilitate this though, for instance this one.
If you’re not setting the cookie on the backend but only from within the browser, you might also consider using the local storage instead, which is much easier to access:
if (localStorage.visited) {/* ... */}
localStorage.visited = 'true'
This will only run into the if branch the first time a user visits your site.