Solved

Help needed setting a cookie based on URL parameter

Posted on 2015-01-16
3
105 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

813 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

15 Experts available now in Live!

Get 1:1 Help Now