www.webdeveloper.com
Results 1 to 2 of 2

Thread: Populate a drop down with Cookie.

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Posts
    1

    Populate a drop down with Cookie.

    I really need help with this. I really don't know how to make this work. I need to populate a drop down menu with a value passed through the browser if it's passed.

    so the browser URL will look like:
    domain.com/?referer=YellowPagesOnline

    I need to capture the referer value from the browser store it into a cookie. If the cookie is set and then populate the drop down on the page with the cookie. If it's not set then a list of sources should appear.

    So I already have the code to grab the string and store it in a cookie but I'm not sure how to do the initialization of the drop down with the cookie value.


    This is what I have...

    Code:
    <script>
    //Get the referer string out of the URL
    function getQuerystring(key, default_)
    {
    
      if (default_==null) default_="";
      key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
      var qs = regex.exec(window.location.href);
      if(qs == null)
        return default_;
      else
        return qs[1];
    }
    
    
    //Set the cookie for 30 days
    function SetCookie(cookieName,cookieValue,nDays) {
     var today = new Date();
     var expire = new Date();
     if (nDays==null || nDays==0) nDays=1;
    /* expire.setTime(today.getTime() + 3600000*24*nDays);*/
     expire.setTime(today.getTime() + (1000 * 60 * 60 * 24 * 30));     
     document.cookie = cookieName+"="+escape(cookieValue)
                     + ";expires="+expire.toGMTString();
    }
    
    
    //Put the cookie on the user's computer
    function putcookie()
    {
    	var val = getQuerystring('referer');
    	
    	if(val !=""){
    		if (document.cookie.length == 0)
    		{
    			SetCookie('referer', val, 1);
    			alert(val);
    		}
    	}
    	alert(document.cookie);
    	
    }
    putcookie();
    
    
    
    
    getCookie(name);
    
    //If there is a cookie then get the cookie
    function getCookie(c_name)
    {
    
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
     
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
    
        if (c_end==-1) c_end=document.cookie.length;
    	
    if(document.SECatJax.referer.length > 0)
    {	
      document.SECatJax.referer.value = unescape(document.cookie.substring(c_start,c_end));
     return false;
    }
     alert(document.cookie);
    
        }
      }
      document.SECatJax.referer.value = 'Website'; 
    }
    The dropdown to appear if there isn't a cookie is
    Code:
    <select name="source_code" size="1" id="source_code" class="select_2" style="display:none">
    <option value="" selected="selected">--</option>
    <option value="Direct Mail">Direct Mail</option>
    <option value="High School Presentation">High School Presentation</option>
    <option value="Search Engine">Internet Search</option>
    <option value="Newspaper">Newspaper</option>
    <option value="Poster">Poster</option>
    <option value="Radio">Radio</option>
    <option value="Referral">Referral</option>
    <option value="TV">TV</option>
    <option value="Yellow Pages">Yellow Pages</option>
    </select>
    The drop down to appear if there is a cookie is
    Code:
    <select name="source_code2" size="1" id="source_code2" class="select_2" style="display:none">
    <option value="YellowPagesOnline" selected="selected">YellowPagesOnline</option>
    </select>
    Can someone help me with this please? Thanks.

  2. #2
    Join Date
    Nov 2010
    Posts
    1,102
    here is a function you can call to populate a dropdown list with a query string:

    Code:
    <body>
    <form name="myform">
    <select name="selectlist">
    </select>
    </form>
    
    <script type="text/javascript">
    function makeList(){
    var refer = unescape(self.location.search.slice(1));
    document.myform.selectlist.options[0]=new Option(refer,refer);
    }
    </script>
    </body>
    Last edited by xelawho; 01-03-2012 at 05:01 PM. Reason: didn't notice it had to be selected

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles