Monthly Archives: October 2014

Using Javascript to get a variable from a URL

Sometimes, within a website, you see variables that are passed through the URL. The variable part of the URL usually looks something like this:
“?tab=mo&authuser=0″

In the above example, ‘tab’ is equal to ‘mo’ and ‘authuser’ is equal to ‘0’.

This snippet below with let you extract the values of variables by parsing the URL. It specifically looks for the & and = symbols so you’ll need to make some changes to the regex part (/[?&]+([^=&]+)=([^&]*)/gi) if you’re using symbols other than that.

function getUrlVar() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars; 
}
var tab = getUrlVar()["tab"];
var user = getUrlVar()["authuser"];

Delete all of a content type in Drupal 7

On a recent project, I had the need to delete thousands of a content type in order for me to correct some issues with the website, before re-adding them all. This is a useful script that I found to do just that. You fill in your content type, and all the nodes will be deleted. This works best in a custom module that you can turn on or off.

$node_types = array('content_type'); // add the machine names of node types you want to bulk delete

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->propertyCondition('type', $node_types, 'IN');
$result = $query->execute();
foreach($result['node'] as $node){
node_delete($node->nid);
}

Find Your Congressperson Drupal Module

This module allows users to lookup a congressperson using a 5 digit US zipcode. It connects to the Sunlight Foundation Congress API V3 (https://sunlightlabs.github.io/congress).

The admin page allows you to toggle on and off the features that you’d like to display.

Here are some screenshots:

drupal module

drupal module
drupal module

You can view this modules on going development on GitHub and on Drupal.org.

Note: This was originally named “Find Your Congressman” because of a clients requirements. I’ve since renamed it “Find Your Congressperson”.

Using javascript to hide/show something on scroll

We’ve probably all seen the little “back to top” buttons that appear on sites once you scroll past the menu or if you at the bottom of a page. This snippet will show you how to slide that out from the right after scrolling down the page.

$(window).scroll(function() {
    if ($(this).scrollTop() > 100) {
        $('.totop').stop().animate({ right: '0px' });
    } else {
        $('.totop').stop().animate({ right: '-300px' });
    }
});
[/cc]

Obviously, you'll need to tell the browser window to "listen" for scrolling.
[cc lang="javascript"]
$(window).scroll(function() {});
[/cc]

Then you'll tell the browser windows, after you scroll 100 pixels down the page, you want to do something.
[cc lang="javascript"]
if ($(this).scrollTop() > 100) {}
[/cc]

The actual animation is next. We're just going to slide this out from the right, but really you could fade it in and out, or side it from the bottom. Any kind of animation would work.
[cc lang="javascript"]
$('.totop').stop().animate({ right: '0px' });