I know there are already a bunch of tools for doing this same thing, but I’m a big google fanboi. The nice thing about goo.gl is they have an API which makes accessing the shortener easy. Matthew Flashen already wrote some JS for a bookmarklet for this. You just save that into your bookmarks, I saved it to my Delicious bookmarks, and you can call it on whatever URL. It alerts you the shortened URL and just copy and paste from there. Sweet!
Archive for the ‘Javascript’ Category
Goo.gl URL shortener bookmarklet
Tuesday, January 26th, 2010Quick email validation regex
Monday, December 7th, 2009So I don’t have to google for this one again, since I won’t remember it. Here is a quick regex to verify that your email address is valid. Should take care of most email addresses.
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i;
Quick and easy mouse over delay with jQuery
Wednesday, April 22nd, 2009On one of my side projects I wanted to do a hover over fade on and off menu, but wanted to do the menu items with text instead of images. After thinking about how to get the effect, I settled on the below solution. I also wanted a slight delay on the mouse over. That way, if someone just went through the links instead of on them, they wouldn’t get the fade.
HTML looks like this. The only drawback is for accessibility, that they get the link text twice. Not that big of a deal IMHO.
<ul>
<li><a href="sports.php"><span class="off">Sports</span><span class="on">Sports</span></a></li>
<li><a href="concerts.php"><span class="off">Concerts</span><span class="on">Concerts</span></a></li>
<li class="last"><a href="theater.php"><span class="off">Theater</span><span class="on">Theater</span></a></li>
</ul>
Then the JS comes in and does a nice smooth fade on the “off” class. This allows you to get a fade rather then an instant “on” like you get when you just change classes and colors on the a element. I do a jQuery hover.
When it does the “mouseover”, it sets a class on the main list item its hovering on, that way the global scope of setTimeout will allow you to target that particular element to fade its child. The 100 ms delay is enough to get rid of most of the inadvertent hovers. If using this particular piece of code for something else, you can make that delay as long as you want for a delayed hover. Then the “off” state fades out.
The “mouseout” from the hover then clears the timeout and fades the “off” state back in and removes the hovering class as you don’t need it anymore.
$('#header li').hover(
function() { $(this).addClass('hover'); hover_intent = setTimeout( function(){ $('#header .hover').find('.off').fadeOut("fast"); } , 100 ); },
function() { try{clearTimeout(hover_intent);}catch(e){}; $('#header .hover').find('.off').fadeIn("fast"); $(this).removeClass('hover'); });
Cross Sub Domain Ajax and JSON
Friday, March 27th, 2009I got a couple comments and emails about my post on cross sub domain JS and AJAX and using an iframe to make that happen. As I was making an example of it so the post made more sense, I thought about the cross sub domain issues we were having at work, and the “duh” solution to the issue of passing data between sub domains. Use JSON instead of XML. The only time you couldn’t use JSON is if you were trying to get a full HTML page instead.
Its absurdly easy with jQuery and there are no iframes or anything else to mess around with. Just setup your js as a function call with a JSON object inside of it with your data.
myData({ "title":"test"})
Then on your page where you want the data, after adding jQuery just run the following
<script type="text/javascript">
function myData(theData) {
alert(theData.title);
}
$.getJSON("http://test.tomhoppe.com/test.js?callback=?");
</script>
This will grab the test.js file containing the function callback and execute that function on your page. At that point you can do whatever you please with your data. Only do this with trusted sources of course, as the JS that you pull in gets immediately executed on your own page.
Easy jQuery fading color menu
Saturday, February 28th, 2009I’m creating a site for a client and wanted to jazz up the main menu slightly. Right now its just a plain text text that rolls over with a new color. It would be cooler, and just as accessible, if I made that css color fade in and out instead of just “appear”. The HTML, CSS, and JS for this with jQuery is easy as pie.
The HTML is below. Notice all I did is double up on the text in span tags. You can’t “fade” the change of a color style, but you can fade the white text in and out above the black text.
<ul>
<li><a href=""><span class="white">Sports</span><span class="black">Sports</span></a></li>
<li><a href=""><span class="white">Concerts</span><span class="black">Concerts</span></a></li>
<li><a href=""><span class="white">Theater</span><span class="black">Theater</span></a></li>
</ul>
CSS. Position that white span tag absolutely so its above the black, and what you see.
#header ul li {
float:left;
font-size:80%;
font-weight:bold;
margin-top:2px;
text-transform:uppercase;
}
#header ul li a {
color:#fff;
display:block;
height:18px;
padding:3px 15px 0 12px;
text-decoration:none;
}
#header .white {
color:#fff !important;
position:absolute;
}
#header .black {
color:#000 !important;
}
The throw in a line of jQuery to manipulate that absolutely positioned white span to fade in and out above the black for a cool fading effect on hover.
$("#header li ").hover(function(){$(this).find('.white').fadeOut("medium");},function(){$(this).find('.white').fadeIn("medium");});
How many times does “x” occur in a string?
Monday, January 5th, 2009Had to figure out how many times a single characted occured in a string today. Found a nice clean way to do that instead of running a loop. Remove all the instances of that letter or character in the string, and the subtract that from the original length of the string. Viola.
You can also adapt this to multi character strings by dividing the result by the amount of chars in your search string. If you look for “asdf” in “1234asdf1234asdf1234asdf”, you will remove 12 characters. Divide that by 4, and you have found “asdf” three times. Two examples below where the result is 3
str = 1234x1234x1234x; str.length - str.replace(/x/gi,'').length;
str = 1234asdf1234asdf1234asdf (str.length - str.replace(/xxxx/gi,'').length)/4
Javascript if then shortcut
Thursday, October 23rd, 2008For some dumb reason, I can’t ever remember this shortcut when I need it. Never again
varName == (value) ? (true) : false;
Get Parameter from Querystring via Javascript (getParam)
Friday, September 5th, 2008A while back I posted a method to grab all the querystrings out of a URL and parse them into arrays. While that is very useful sometimes, its overkill for other situations, when you have 1 or 2 variables in the querystring and you know their names already. For those times, I like to use a function “getParam”. It takes in the variable name and returns you its value from the querystring.
function getParam(name) {
var regexS = "[\\?&]"+name+"=([^]*)";
var regex = new RegExp( regexS );
var tmpURL = window.location.href;
var results = regex.exec( tmpURL );
if( results == null )
return "";
else
return results[1];
}
2 ways of Javascript menu or navigation highlighting
Wednesday, August 27th, 2008The following two ways are the way I do client side javascript menu highlighting. These work well if you have a site navigation or menu and you want to dynamically highlight the page that you are currently on. I use Method 1 when I have only a few items in the nav, as the JS for it is super easy, or I need to have relative URLs in my menu. Method 2 is better when there are a lot of items, as it relies only on the href and url and doesn’t need any extra stuff in the HTML. It does need absolute URLs in the menu href’s though in order to match correctly with the url.
Method 1 - This method relies on the fact that you will put id’s on each of the a elements with the value of the file name of that file. The HTML shows what I mean. The JS then just looks at the file name of the URL and makes that ID active. Super easy. There is also an “else” statement at the end of the JS so if the file name is blank because you are on “http://yourdomain.com/” it goes ahead and highlights the index menu item.
<div id="topMenu">
<ul>
<li><a id="index" href="">Blog</a></li>
<li><a id="about" href="about.html">About</a></li>
<li><a id="race_car" href="race_car.html">Race Car</a></li>
<li><a id="contact" href="contact.html">Contact</a></li>
</ul>
</div>
<script type="text/javascript" language="javascript">
var fileName=location.href.toLowerCase().substring( location.href.lastIndexOf("/")+1 ).split('.')[0];
if (document.getElementById(fileName)) {document.getElementById(fileName).className = "active";}
else {if (fileName == '') {document.getElementById('index').className = "active";}}
</script>
Method 2 - This method does not need any id’s in the a elements. What it does it look through the href statements in your menu and compares them to the URL. This method works better for larger menus.
<div id="topMenu">
<ul>
<li><a href="http://www.tomhoppe.com/test/index.html">Blog</a></li>
<li><a href="http://www.tomhoppe.com/test/about.html">About</a></li>
<li><a href="http://www.tomhoppe.com/test/race_car.html">Race Car</a></li>
<li><a href="http://www.tomhoppe.com/test/contact.html">Contact</a></li>
</ul>
</div>
<script type="text/javascript" language="javascript">
var theUrl = location.href.toLowerCase();
var navLinks = document.getElementById('topMenu').getElementsByTagName('a');
if (theUrl == 'http://www.tomhoppe.com/test/') { navLinks[0].className = 'active'; }
else {
for (var i=0; i<navLinks.length; i++) {
var NavLinkUrl = navLinks[i].getAttribute('href').toLowerCase();
if (NavLinkUrl == theUrl) {navLinks[i].className = 'active';}
}
}
</script>
Work work work work …….
Wednesday, August 20th, 2008I’ve noticed that after working a TON, as in 80+ hour weeks for a few weeks, when that stops and I go back to “normal”, I have a real hard time adjusting to “normal” life. I get home and its as if my brain is used to still running at 100mph and not being able to just “sit still”. Real wierd feeling. Almost as if I’m lost just relaxing, and always feeling like its wierd that I’m not “doing something”.
Or maybe I just need a vacation ……