Solved

Help needed setting a cookie based on URL parameter

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.

746 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

11 Experts available now in Live!

Get 1:1 Help Now