Solved

Help needed setting a cookie based on URL parameter

Posted on 2015-01-16
3
104 Views
Last Modified: 2015-01-20
I have a email signup modal located within different sections of my site. The modals contain a button to force the modal to not be displayed again. This is done by setting a cookie.

So for example I have modals located here:
/section1/home
/section1/articles
*section1 has a url parameter of sectionID=1

/section2/home
/section2/articles
*section2 has a url parameter of sectionID=2

Is there any way to set the cookie path based on the url parameter? That way if it's set within the section, the modal won't display on /home or /articles?

This is what I'm using to set the cookie.
     $.cookie('dismiss_newsletter', 'true', { path : '/', expires : 10*365 });
0
Comment
Question by:jej07
3 Comments
 
LVL 50

Expert Comment

by:Steve Bink
ID: 40555264
Can you explain a little more about what you want to do?  Any cookie you set with path:'/' should be accessible on the next page load across your entire site.
0
 
LVL 14

Accepted Solution

by:
quizwedge earned 500 total points
ID: 40555337
To get the URL parameter, check out the code taken from http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++)
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam)
        {
            return sParameterName[1];
        }
    }
}​

Open in new window


You could then get sectionID by calling

var sectionID = GetURLParameter('sectionID');

Open in new window


You should then be able to say

$.cookie('dismiss_newsletter', 'true', { path : sectionID, expires : 10*365 });

Open in new window

0
 
LVL 1

Author Closing Comment

by:jej07
ID: 40560938
Thank you that was a huge help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now