|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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>
|
|
#2
|
|||
|
|||
|
How about this:
Code:
String.prototype.splitAtChar=function() {
return this.split(this.charAt(this.indexOf(arguments[0])));
}
Code:
str = 'abc|defdef|ghighighi';
tmp = str.splitAtChar('|');
alert(str+'\n\nsplitAtChar("|"): '+tmp+'\n\n'+tmp.join('\n'));
|
|
#3
|
|||
|
|||
|
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? |
|
#4
|
|||
|
|||
|
Code:
var str = 'BALTIMORE, MD|blah blah|blah blah|blah blah' var state = str.match(/,\s(\w+)\|/); alert(state[1]); |
|
#5
|
||||
|
||||
|
Code:
var str = 'BALTIMORE, MD|blah blah|blah blah|blah blah'
alert(str.replace(/.*\s([A-Z]{2}).*/g,"$1"));
__________________
libs: mini (updated) ASPmini myIO (new) dnd tmpl8 apps: snippets blog photos crypto image editor crapplets: json browse json view compressor time grads |
|
#6
|
|||
|
|||
|
Thanks so much guys! the regular expression worked great!
Cheers! |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|