Wednesday, September 22, 2010

JSON Fetch Random Record

Searched everywhere for this one. Hopefully it will help someone else out...

I was looking for a way to filter a JSON dataset by an ID number. Here goes...


  1. Step 1 : Create your JSON Data.


    var item = [
    { "id": "0", "language": "English"},
    { "id": "1", "language": "Spanish"},
    { "id": "2", "language": "French"},
    { "id": "3", "language": "Hungarian"}
    ];

  2. Step 2 : Function to find Unique Value:


    I cant take credit for this one. I found this at : http://stackoverflow.com/questions/604935/how-can-i-filter-json-for-unique-key-name-value-pairs. Here it is...

    function getDistinct(o, attr) {
    var answer = {};
    $.each(o, function(index, record) {
    answer[index[attr]] = answer[index[attr]] || [];
    answer[index[attr]].push(record);
    });
    return answer;
    }

  3. Step 3: Create Random Number

    var randNo = Math.floor(Math.random() * 4); (with 4 being the same number of records contained in the JSON Dataset).

  4. Step 4: Return unique record items


    $(function() {
    $.each(getDistinct(markers, "id"), function(groupName, recordArray) {
    var firstRecord = recordArray[randNo];
    $('
  5. ').html(firstRecord.language).appendTo('#results');
    });
    });




Finally, the body simply contains an unordered list with an id of results to contain the information.

Friday, September 17, 2010

ASP.NET Routing 4.0 Handle Error

OK, so this took a lot of searching, but this worked. Not sure why it isn't put into the Web.Config file initially, or that there isn't more documentation, as this had me going nuts. Anyways, if you get a 404 error on the ASP.NET 4.0 routing feature when you deploy to IIS7.0, take a look at this. I bet it will get you going...

http://www.gildnersolutions.com/post/2010/04/21/ASPNET-40-URL-problem-with-IIS7.aspx

Tuesday, July 13, 2010

Javascript In or Out of Office

Wanted to create a standard tool to determine if a business is in or out of the office. Fairly standard stuff, but here it is if anyone can use it.


Needed to check for 3 things.


  1. Holidays

  2. Weekends

  3. Business Hours (in this case 8 to 5)




Anyways, here's the script that results in a boolean value to determine whether or not the office is open.


var d = new Date();
var day = d.getDay();
var h = d.getHours();
var y = d.getYear();

var holidays = ['1/1/' + y,
'1/21/' + y,
'2/18/' + y,
'5/26/' + y,
'7/4/' + y,
'9/1/' + y,
'10/13/' + y,
'11/11/' + y,
'11/27/' + y,
'12/25/' + y,
'12/31/' + y];

function isWeekend() {
if (day == 0 || day == 6) {
return true;
}
else
{
return false;
}
}

function isOffHours() {
if (h < 8 || h > 17) {
return true;
}
else {
return false;
}
}

function businessClosed() {
var today = (d.getMonth() + 1) + '/' + d.getDate() + '/' + y;
if (isWeekend() == true || (jQuery.inArray(today, holidays)) > 0 || isOffHours() == true) {
return true;
}
else {
return false;
}
}

$(function () {
if (businessClosed() == true) {
alert('Sorry, we are closed!');
}
else {
alert('Open for Business!');
}
});


If you want to download, it can be found HERE.

Thursday, June 17, 2010

C# vs VB - The debate continues...

Just getting my feet wet with C#, after being a VB developer for awhile, realizing that C# is getting all of the jobs. With that being said, have run into a few difficulties in converting over, and I'll list a couple here, to be added to as I go along. Consider this a newbie's version of headaches you may face when switching to C#...




  1. In VB, if you want to locate an element inside of a control, you would use:

    Dim control as CONTROLTYPE = CType(CONTAINER.FindControl("ControlID"), CONTROLTYPE)


    In C#, you would write the same expression as:

    CONTROLTYPE control = (CONTROLTYPE)CONTAINER.FindControl("ControlID");

  2. In VB, if you want to place an IIf statement inside of a Label Control, you're free to do so. Simply use

    <%#IIf(Convert.ToString(EXPRESSION)=Something, True, False)%>

    However, when you attempt to use this in C#, you will get an error, as IIf isn't recognized.

    Here's the fix:

    <%# (Convert.ToString(EXPRESSION) == Something ? True : False)%>.

  3. Of Course, & is not recognized, it is replaced by +.

  4. I know when I was creating a VB Side HTML Message, or if I was creating a very long string, I would use '& vbCrLf _'. However, in C#, this changes a bit. Declare your String, and append it just as you would in JS. Example?


    string A = "";

    A += "This is a string ";
    A += " and the second line of the string...";



I will modify this as I go, but if you are like me, and have done VB this whole time, I would recommend taking any current sites you have done, and redo them from scratch in C#. Get Hung Up? Use THIS as a last resort, it's a pretty good tool.


Good Luck!

Wednesday, June 9, 2010

Thank You Ryan Grove - Finally a Script Loader that works painlessly!

Finally was able to make my homepage not CRAWL due to a lazy script loader that I found. Prior to this, either security was too tight from GoDaddy or it was just made for Image Files. Anyways, check this out...


http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload

Tuesday, June 8, 2010

Use Google Calendar as Datasource for Holiday Notifications

I wanted to see how difficult it would be to do a simple notification on my site of any basic holidays that occurred on any given day. Sure, it would be simple enough to simply create an array to hold the data in js, and display if the date matched, but I figured I would use a JSON Feed to display the data instead. Turns out Google Calendar has just the thing in their Holiday Calendar Feed.

http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/basic

If you go to this feed, you'll notice there is a lot of information that needs to be parsed down to use it, so after some fiddling, I came up with the following YQL Feed:

select entry.title.content, entry.summary.content from xml where url='http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/basic' AND entry.content.content LIKE '% + year + %'

with year being a new Date().getYear() value.

Anyways, here's the script. It simply displays a message in a Div if today falls on one of the holidays.

$(function () {
var d = new Date();
var year = d.getYear();
var shortDay = (d.toDateString()).slice(4, 10);
var query = "select entry.title.content, entry.summary.content from xml where url='http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/basic' AND entry.content.content LIKE '%" + year + "%'";
$.getJSON(
"http://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(query) + "&format=json&callback=?",
function (data) {
$.each(data.query.results,
function (index, item) {
for (i = 0; i < item.length; i++) {
var each = item[i];
var date = ((each.entry.summary).slice(9, 16).replace(',', ''));
if (jQuery.trim(date) == jQuery.trim(shortDay)) {
$('<h6></h6>').html('Today is ' + each.entry.title + '!').appendTo('#holidays');
}
}
});
});
});

You can feel free to download this script if you would like to use it.

http://www.joshuablackstone.com/Scripts/HappyHolidays.js.

Friday, June 4, 2010

Using YQL and Google Translate to Translate Text

Had a little time to play, so I wanted to see what I could do to internationalize (I think that's a word :) ) my site. I noticed in YQL there was a GoogleTranslate Table, so after some trial and error, found out that with:

select * from json where url='http://www.google.com/uds/Gtranslate?context=22&langpair=%7C" + lang + "&v=1.0&q=" + encodeURIComponent(str) + "'

with the variables being, of course, lang for language symbol (i.e. en for english, de for dutch, etc.) and str for Text String to be translated. Plug that into a YQL Json function and you're set, well, almost...

Why not also scrape the items from the Google Site to use for a Combo Box to select a language from? If you go to: http://translate.google.com, you will see a combo box next to TranslateInto. To Get the data from that, simply use this query as your JSON String:

http://query.yahooapis.com/v1/public/yql?q=select%20option%20from%20html(0)%20where%20url%3D%22http%3A%2F%2Ftranslate.google.com%22%20and%0D%0A%20%20%20%20%20%20xpath%3D'%2F%2Fselect%5B%40class%3D%22tllangdropdown%22%5D'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=?

Plug that into the YQL Console, and you will find that you can get the value and Content for items to plug into a combo box of your own.

Finally, wire up the dropdown that was created with an event similar to:

$("#DROPDOWNNAME").change(function () {
translateText($(str).html(), this.value);
});

And you're all done.

I realize this was a very brief explaination, but for the full script simply go to:

http://www.joshuablackstone.com/Scripts/GTranslate.js.