www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-20-2009, 02:43 PM
    bigalo bigalo is offline
    Registered User
     
    Join Date: Apr 2008
    Location: North Carolina
    Posts: 39
    Question String Object Method Help

    Hello,

    I was able to solve an issue I had in previous post in writing some code to grab a section of a cookie value string (2 letter state ex MD) and check against it to do something. That was easy because the state was at the end of the string and all I had to do was use the slice() method. Now I was to be able to grab the 2 letter state from a string that looks like this:

    BALTIMORE, MD|blah blah|blah blah|blah blah (the real cookie value string will always be separated with pipes (|))

    Can anyone please help?

    Thanks in advance!

    Code:
    <HTML>
    <HEAD>
    <TITLE></TITLE>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <SCRIPT LANUAGE="JavaScript">
    
    function setCookie(name, value, expires, path, domain, secure) {
        document.cookie= name + "=" + escape(value) +
            ((expires) ? "; expires=" + expires.toGMTString() : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
    }
    function getCookie(name) {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        } else {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
            end = dc.length;
        }
        return unescape(dc.substring(begin + prefix.length, end));
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <script>
     $(function(){ 
    	 $('div').each(function(){
    		 if($(this).hasClass('stateSelect1')){ 
    		 	 if (getCookie("location")!=null){
    			 var state = getCookie("location").slice(-2).toLowerCase();
    			 var stateArray = [];
    			 stateArray = $(this).attr('rel').toLowerCase().split(',');
    				 if($.inArray(state,stateArray) >= 0){ 
    				 $(document).ready(function(){
    				 	$(".stateSelect0").css("display","none");
    					$(".stateSelect1").css("display","block");
    				  });
    				 }
    				}  	 
    		 	} 
    	 }); 
     });
     </script>
    <strong>Step 1.  Copy and paste in cookie set text field:</strong> <br>
    <br>
    <strong>Show Image1:</strong><br>
    CHARLOTTE, NC|blah blah|blah blah|blah blah|blah blah<br>
    BALTIMORE, MD|blah blah|blah blah|blah blah<br>
    <br>
    <strong>Show Image2:</strong><br>
    COLUMBIA, SC|blah blah|blah blah|blah blah<br>
    RESTON, VA|blah blah|blah blah|blah blah<br>
    <br>
    <strong>Show Image3:</strong><br>
    LOS ANGELES, CA|blah blah|blah blah|blah blah<br>
    SEATLE, WA|blah blah|blah blah|blah blah<br>
    <br>
    <br>
    <strong>Step 2.</strong>
    <input type="button" value="Set Cookie"  onclick='setCookie("location", prompt("Enter your location"))' />
    <br>
    <br>
    <strong>Step 3.</strong> Now refresh page. <br>
    <br>
    <style>
    .default{ border:3px solid limegreen; margin-bottom:5px; width:200px}
    .div1{ border:3px solid red; margin-bottom:5px; width:200px}
    .div2 { border:3px solid purple; margin-bottom:5px; width:200px}
    .div3 { border:3px solid yellow; margin-bottom:5px; width:200px}
    .div4 { border:3px solid blue; margin-bottom:5px; width:200px}
    </style>
    <!--DEFAULT IMAGE IF NO COOKIE SET OR NON LISTED STATE-->
    <div class="stateSelect0 default"> DEFAULT IMAGE </div>
    <!--DEFAULT IMAGE IF NO COOKIE SET OR NON LISTED STATE-->
    <div class="stateSelect1 div1" rel="NC,MD" style="display:none">Image 1 - DIV 1</div>
    <div class="stateSelect2 div2" rel="SC,VA" style="display:none">Image 2 - DIV 2</div>
    <div class="stateSelect3 div3" rel="WA,CA" style="display:none">Image 3 - DIV 3</div>
    </BODY>
    </HTML>
    Reply With Quote
      #2  
    Old 11-20-2009, 03:26 PM
    JMRKER JMRKER is offline
    Registered User
     
    Join Date: Dec 2005
    Location: FL
    Posts: 4,563
    Lightbulb Consider this ...

    How about this:
    Code:
    String.prototype.splitAtChar=function() {
    	return this.split(this.charAt(this.indexOf(arguments[0])));
    }
    Called via following test code:
    Code:
            str = 'abc|defdef|ghighighi';
            tmp = str.splitAtChar('|');
        	alert(str+'\n\nsplitAtChar("|"): '+tmp+'\n\n'+tmp.join('\n'));
    Reply With Quote
      #3  
    Old 11-20-2009, 03:40 PM
    bigalo bigalo is offline
    Registered User
     
    Join Date: Apr 2008
    Location: North Carolina
    Posts: 39
    So, if my string looked like this:

    BALTIMORE, MD|blah blah|blah blah|blah blah

    How doI grab the MD (or any 2 letter state) and maybe alert that?
    Reply With Quote
      #4  
    Old 11-20-2009, 03:53 PM
    Nedals Nedals is offline
    Registered User
     
    Join Date: Dec 2002
    Location: Pleasanton, CA
    Posts: 2,089
    Code:
    var str = 'BALTIMORE, MD|blah blah|blah blah|blah blah'
    var state = str.match(/,\s(\w+)\|/);
    alert(state[1]);
    Reply With Quote
      #5  
    Old 11-21-2009, 03:34 AM
    rnd me's Avatar
    rnd me rnd me is offline
    working on the chain...
     
    Join Date: Jul 2008
    Location: urbana, il
    Posts: 1,504
    Code:
    var str = 'BALTIMORE, MD|blah blah|blah blah|blah blah'
    alert(str.replace(/.*\s([A-Z]{2}).*/g,"$1"));
    Reply With Quote
      #6  
    Old 11-23-2009, 08:48 AM
    bigalo bigalo is offline
    Registered User
     
    Join Date: Apr 2008
    Location: North Carolina
    Posts: 39
    Thanks so much guys! the regular expression worked great!


    Cheers!
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:18 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.