Friday, February 4, 2011

Friday Feb. 4 2010

Been working on a lot of projects, lots of snow.


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Replacement Insanity</title>
<script>
window.onload = function(){
// search for \d
var re = /\\d/;
var pattern = "\\d{4}";
var str = "I want 1111 to find 3334 certain 5343 things 8484";
var re2 = new RegExp(pattern, "g");
var str1 = str.replace(re2, "*****");
alert(str1);
var pattern2 = pattern.replace(re, "\\D");
var re3 = new RegExp(pattern2, "g");
var str2 = str.replace(re3, "****");
alert(str2);
}

</script>
</head>
<body>
<p>content</p>
</body>
</html>


Here is another program from teh Shelley Powers book



var dt = new Date();

// get month and increment
var mnth = dt.getUTCMonth();
mnth++;
alert(mnth);

var day = dt.getUTCDate();
if (day < 10) day="0" + day;
var yr = dt.getUTCFullYear();
alert(yr );

var hrs = dt.getUTCHours();
if (hrs < 10) hrs = "0" + hrs;
alert(hrs);

var min = dt.getUTCMinutes();
if (min < 10) min = "0" + min;
alert(min);

var secs = dt.getUTCSeconds();
if (secs < 10) secs = "0" + secs;
alert(secs);


var newDate = yr + "-" + mnth + "-" + day + "T" + hrs + ":" + min;

alert(newDate);




Here is another Shelley Powers tutorial. This one I could nto quite get to work - I got some IS0 8601 dates from wikipedia, and it was fine with teh date that did not have a time added, but for the date with time it kept saying that it was invalid.



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Converting IS0 8601 Date</title>
<!-- note: this is from a tutorial by Shelley Powers, from her book "JavaScript Cookbook". I am using this for personal educational purposes only -->
<style type="text/css">
#dateSubmit
{
background-color: #ff0;
width:200px;
text-align: center;
border: 1px solid #ccc;
}
</style>
<script type = "text/javascript">
window.onload = function () {
document.getElementById("dateSubmit").onclick = convertDate;
}

function convertDate() {
var dtstr = document.getElementById("datestring").value;
var convdate = convertISO8601toDate(dtstr);
document.getElementById("result").innerHTML = convdate;
}

function convertISO8601toDate(dtstr) {

//replace anything but numbers by spaces
dtstr = dtstr.replace(/\D/g," ");

//trim any hanging white space
dtstr = dtstr.replace(/\s+$/,"");

// split on space
var dtcomps = dtstr.split(" ");

if (dtcomps.length < 3) return "invalid date";
// if time not provided, set to 0
if (dtcomps.length < 4) {
dtcomps[3] = 0;
dtcomps[4] = 0;
dtcomps[5] = 0;
}


//modify month between 1 based ISO 8601 and zero based date
dtcomps[1]--;

var convdt = new Date(Date.UTC(dtcomps[0], dtcomps[1], dtcomps[2], dtcomps[3], dtcomps[4], dtcomps[5]));

return convdt.toUTCString();
}
</script>
</head>
<body>
<form>
<p>Datestring in ISO 8601 format:
<input type="text" id="datestring" /></p>
</form>
<div id="dateSubmit">
<p>Convert Date
</p></div>
<div id="result"></div>

</body>
</html>


here is a timeout


window.onload=function() {
setTimeout("alert('timeout')",3000);
}


Here is the script and style from the next Shelley Powers tutorial.


<style>
#redbox
{
position: absolute;
left:100px;
top: 100px;
width:200px;
height:200px;
background-color:red;
}
</style>


<script type="text/javascript">
var intervalid=null;

window.onload=function() {
document.getElementById("redbox").onclick=stopStartElement;
}

function stopStartElement(){
if (intervalid == null) {
var x = 100;
intervalid = setInterval(function(){
x += 5;
var left = x + "px";
document.getElementById('redbox').style.left = left;
}, 100);
}
else {
clearInterval(intervalid);
intervalid = null;
}
}

</script>

No comments: