Skip to content

Instantly share code, notes, and snippets.

@firedfox
Last active March 30, 2016 15:36
Show Gist options
  • Save firedfox/731505a4b76be30caf5c to your computer and use it in GitHub Desktop.
Save firedfox/731505a4b76be30caf5c to your computer and use it in GitHub Desktop.
format url for phantom
/**
* @file format url for phantom
* @author firedfox
*/
/*
* format url before opened by a WebPage instance
*
* @param {string} url web page url
* @return {string} url formatted
*/
var formatURL = function (url) {
// removes whitespace
url = url.trim();
// encode non-ASCII characters. necessary for urls including Chinese / Japanese, etc.
url = url.split('').map(function (character) {
return character.charCodeAt(0) <= 255 ? character : encodeURIComponent(character);
}).join('');
// add default protocol
if (url.indexOf('http://') !== 0 && url.indexOf('https://') !== 0) {
url = 'http://' + url;
}
return url;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment