It can be done using code like the following.
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var googleFontUrl = 'https://fonts.googleapis.com/css?family=Droid+Serif:400,700,700i|Merriweather:300,400,700,900i|Open+Sans+Condensed:300,300i,700';
var fontFamilies = getParameterByName('family', googleFontUrl).split('|');
var fontArr = [];
fontFamilies.forEach(function(item) {
fontArr.push(item.split(':')[0]);
});
alert(fontArr.join(', '));
JSFiddle: https://jsfiddle.net/5qvpg606/
Explanation:
First, retrieve value of the family
parameter from the URL using the getParameterByName
function. In this example, the value of the family
parameter is "Droid Serif:400,700,700i|Merriweather:300,400,700,900i|Open Sans Condensed:300,300i,700"
Then split the value into an array to get the font families. We use JS String .split() method with "|" as the separator. Now items in the array are: "Droid Serif:400,700,700i", "Merriweather:300,400,700,900i", and "Open+Sans Condensed:300".
Loop through the array, split each item (string) using ":" as the separator, get the first part (which contains family name) and push it to fontArr
, so you'll get an array that contains "Droid Serif", "Merriweather", and "Open Sans Condensed".
To see the result, simply join the array: fontArr.join(', ')