Retrieving URL Parameter Values With Pure Javascript

Here is a short but incredibly useful piece of Javascript: it retrieves the value of a named URL parameter. For example, if the current page’s address is index.html?example=true&name=vinny this function would return vinny if passed the name name , and it would return true if passed the name example .

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

A note of warning: calling this method repeatedly is inefficient, since the regular expression needs to be repeated every time.

Cloud Integration: An Error Occurred When Creating The Project

While integrating an App Engine application into a Google Cloud project, you may experience the below error:

Here is the transcribed form:

Cloud Integration
Create a Google Cloud project for this application.
An error occurred when creating the project. Please retry.

While this error is rare, it can easily be fixed. Press the Retry button to try integrating the project again; this will solve the vast majority of failures. If this error occurs repeatedly, you may wish to file an issue on the tracker. There are some integration errors that are documented, such as issue 9602: https://issuetracker.google.com/issues/35895752  .

In the interim – until integration succeeds – App Engine applications can be configured to use assets and services that are technically within a separate project. For example, GAE applications can access Cloud Storage buckets that are listed in a separate project as long as their service account names are listed in the bucket ACLs.

App Engine 1.8.6 Rollout

App Engine recently upgraded to version 1.8.6 in production, and the most obvious change is the errors summary screen.

Here’s the Errors screen prior to 1.8.6:

The current 1.8.6 Errors screen breaks down the errors into client and server-caused issues:

The newly added Error Details graph shows when different errors occur. For instance, this graph shows a client error (an incorrect request) about 19 hours ago:

Aliasing Imports In Golang

A quick note: Go allows the aliasing of imports. For instance, the below line of code imports the appengine/datastore package under the name aedatastore :

import aedatastore "appengine/datastore"

Then the application can call datastore services under the alias name:

aedatastore.Get(c, key, &entity)

This aliasing is particularly useful when package names collide. Assume that an application needs to import the Go image and appengine/image packages. Since both packages are named image, the Go compiler will issue an image redeclared as package name error if both are imported. To avoid this, alias one of the imports:

import "image"
import aeimage "appengine/image"

Webmaster Tool Has Not Been Enabled

While configuring a new Google Apps account for a customer, I encountered the error below:

Here’s how to fix this. First, go to the Users tab, then click Services :

Type in Webmaster Tools in the search box directly under the Services tab:

Click the On button next to the Webmaster Tools label:

Remember to save your changes:

Retrieving The User’s IP Address

Retrieving the originating IP address of the request is simple in most languages.

Here’s how to retrieve the IP address in Java ( req represents a javax.servlet.http.HttpServletRequestreference ):

String ip = req.getRemoteAddr();

Here’s the same line in Go ( r represents http.Request ):

ip := r.RemoteAddr

In PHP, the originating IP can be retrieved from the predefined variable SERVER :

$_SERVER['REMOTE_ADDR']

JSON, Javascript, and JSONP MIME Types

A quick note about MIME types: JSON responses have a MIME type of application/json while Javascript files use application/javascript . JSONP is a JSON object within a Javascript function call, so it shares the same MIME type as Javascript ( application/javascript ).

With that said, there are some browsers (largely older browsers) and applications that don’t understand the application/json MIME type. They may require JSON responses to have a content type of application/javascript , application/x-javascript , or text/html . If you encounter issues with handling JSON, it’s a good idea to try changing the MIME type – it may solve the problem.

As a reminder, here’s how to set the content type of a response in Java (other languages have similar functions):

resp.setContentType(mime_type);

The resp object represents a javax.servlet.http.HttpServletResponse reference.

Google Services Modifying Site Navigation

A quick note about some Google UI changes: Google is in the middle of removing the top black navigation bar.

Previously, Google had a top nav bar linking to various Google services:

Now that navigation bar is gone:

Google is popping up a little note to explain the changes:

In short, the grid icon drops down a list of various Google services to choose from:

Returning Status Codes In Golang

Here’s an overview of how to send HTTP status codes in Go. W represents a http.ResponseWriterreference, and r is a http.Request reference.

The following line tells the browser to redirect to the URL given (a 301 is a Moved Permanently redirect):

http.Redirect(w, r, "http://learntogoogleit.com/", 301)

Here is a standard Not Found error:

http.Error(w, http.StatusText(404), 404)

An Internal Server Error is indicated using a 500 status code:

http.Error(w, http.StatusText(500), 500)

The above examples use StatusText to retrieve standard error text, but you can set your own text if needed:

http.Error(w, "Too Many Requests", 429)