I'm trying to dig into the following array, to turn all of the connector type items into select options when I have parsed it.
At the moment, I can get all the data I require, but I'm wanting to turn each item returned into a single string as opposed to having it return items that have more than one connector type as a single item (beneath example shows what I currently have / desire. That and only show unique types, so if the connector name has been showed once, don't show that name again as an option.
For example (current output):
(Shows duplicates, nested items with more than one type don't break onto single lines)
ConnectorType1
ConnectorType1, ConnectorType2, ConnectorType3
ConnectorType1
ConnectorType1, ConnectorType2
Desired output:
(Shows unique items only, all results broken onto new lines)
ConnectorType1,
ConnectorType2,
ConnectorType3
Pastebin with JSON example:
{
"ChargeDevice": [
{
"ChargeDeviceId": "cfeedcdd5e287bef4b583158a12363f1",
"ChargeDeviceRef": "SRC_LDN60188",
"ChargeDeviceName": "2 Riddons Road",
"ChargeDeviceText": null,
"ChargeDeviceLocation": {
"Latitude": "51.431454",
"Longitude": "0.031175",
"Address": {
"SubBuildingName": null,
"BuildingName": "",
"BuildingNumber": "",
"Thoroughfare": "Riddons Road",
"Street": "Junction with Chinbrook Road",
"DoubleDependantLocality": null,
"DependantLocality": null,
"PostTown": "Leek",
"County": "Greater London",
"PostCode": "SE12 9QR",
"Country": "gb",
"UPRN": null
},
"LocationShortDescription": null,
"LocationLongDescription": ""
},
"ChargeDeviceManufacturer": null,
"ChargeDeviceModel": null,
"PublishStatusID": "1",
"DateCreated": "2014-08-19 05:15:02",
"DateUpdated": "2015-09-02 11:28:16",
"Attribution": "Source London",
"DateDeleted": "n/a",
"Connector": [
{
"ConnectorId": "1",
"ConnectorType": "3-pin Type G (BS1363)",
"RatedOutputkW": "3.7",
"RatedOutputVoltage": "230",
"RatedOutputCurrent": "16",
"ChargeMethod": "Single Phase AC",
"ChargeMode": "1",
"ChargePointStatus": "In service",
"TetheredCable": "0",
"Information": " x 3-pin square (BS 1363) - Standard (up to 3.7kW, 13-16A)",
"Validated": "0"
},
{
"ConnectorId": "2",
"ConnectorType": "Type 2 Mennekes (IEC62196)",
"RatedOutputkW": "7.0",
"RatedOutputVoltage": "230",
"RatedOutputCurrent": "32",
"ChargeMethod": "Single Phase AC",
"ChargeMode": "3",
"ChargePointStatus": "In service",
"TetheredCable": "0",
"Information": " x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
"Validated": "0"
}
],
"DeviceOwner": {
"OrganisationName": "Source London",
"SchemeCode": "SRC_LDN",
"Website": "https://www.sourcelondon.net",
"TelephoneNo": "020 3056 8989"
},
"DeviceController": {
"OrganisationName": "Source London",
"SchemeCode": "SRC_LDN",
"Website": "https://www.sourcelondon.net",
"TelephoneNo": "020 3056 8989"
},
"DeviceAccess": [],
"DeviceNetworks": "Source London",
"ChargeDeviceStatus": "In service",
"PublishStatus": "Published",
"DeviceValidated": "0",
"RecordModerated": "Y",
"RecordLastUpdated": "2015-09-02 11:28:16",
"RecordLastUpdatedBy": "NCR Admin",
"PaymentRequiredFlag": false,
"PaymentDetails": "",
"SubscriptionRequiredFlag": true,
"SubscriptionDetails": "\u00a35 per annum for RFiD card",
"ParkingFeesFlag": false,
"ParkingFeesDetails": "",
"ParkingFeesUrl": null,
"AccessRestrictionFlag": false,
"AccessRestrictionDetails": "",
"PhysicalRestrictionFlag": false,
"PhysicalRestrictionText": "",
"OnStreetFlag": true,
"LocationType": "On-street",
"Bearing": null,
"Accessible24Hours": false
}
]
}
Current code for looping through JSON:
for (let x = 0; x < data.ChargeDevice[i].Connector.length; x++) {
if (connectors.indexOf(data.ChargeDevice[i].Connector[x].ConnectorType) === -1) {
connectors.push(data.ChargeDevice[i].Connector[x].ConnectorType);
$('#connectorList').append(`<option data-loc-name="${connectors}" value="${connectors}">${connectors}</option>`);
}
}
I would suggest you to loop through all connection with Array.from(myJson.ChargeDevice[0].Connector, ....
Then for each connection, you push the value of .ConnectorType into an array (myConnArr) if it is not already present. Like this if(!myConnArr.includes(conn.ConnectorType)) myConnArr.push(conn.ConnectorType)
Lastly, I join all the result and separate them like this .join(", \n").
The full code snippet. For the test purpose, I duplicate some connector value in order to show remove_duplicates() works fine.
let myJson = {
"ChargeDevice": [
{
"ChargeDeviceId": "cfeedcdd5e287bef4b583158a12363f1",
"ChargeDeviceRef": "SRC_LDN60188",
"ChargeDeviceName": "2 Riddons Road",
"ChargeDeviceText": null,
"ChargeDeviceLocation": {
"Latitude": "51.431454",
"Longitude": "0.031175",
"Address": {
"SubBuildingName": null,
"BuildingName": "",
"BuildingNumber": "",
"Thoroughfare": "Riddons Road",
"Street": "Junction with Chinbrook Road",
"DoubleDependantLocality": null,
"DependantLocality": null,
"PostTown": "Leek",
"County": "Greater London",
"PostCode": "SE12 9QR",
"Country": "gb",
"UPRN": null
},
"LocationShortDescription": null,
"LocationLongDescription": ""
},
"ChargeDeviceManufacturer": null,
"ChargeDeviceModel": null,
"PublishStatusID": "1",
"DateCreated": "2014-08-19 05:15:02",
"DateUpdated": "2015-09-02 11:28:16",
"Attribution": "Source London",
"DateDeleted": "n/a",
"Connector": [
{
"ConnectorId": "1",
"ConnectorType": "3-pin Type G (BS1363)",
"RatedOutputkW": "3.7",
"RatedOutputVoltage": "230",
"RatedOutputCurrent": "16",
"ChargeMethod": "Single Phase AC",
"ChargeMode": "1",
"ChargePointStatus": "In service",
"TetheredCable": "0",
"Information": " x 3-pin square (BS 1363) - Standard (up to 3.7kW, 13-16A)",
"Validated": "0"
},
{
"ConnectorId": "1",
"ConnectorType": "3-pin Type G (BS1363)",
"RatedOutputkW": "3.7",
"RatedOutputVoltage": "230",
"RatedOutputCurrent": "16",
"ChargeMethod": "Single Phase AC",
"ChargeMode": "1",
"ChargePointStatus": "In service",
"TetheredCable": "0",
"Information": " x 3-pin square (BS 1363) - Standard (up to 3.7kW, 13-16A)",
"Validated": "0"
},
{
"ConnectorId": "2",
"ConnectorType": "Type 2 Mennekes (IEC62196)",
"RatedOutputkW": "7.0",
"RatedOutputVoltage": "230",
"RatedOutputCurrent": "32",
"ChargeMethod": "Single Phase AC",
"ChargeMode": "3",
"ChargePointStatus": "In service",
"TetheredCable": "0",
"Information": " x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
"Validated": "0"
}
],
"DeviceOwner": {
"OrganisationName": "Source London",
"SchemeCode": "SRC_LDN",
"Website": "https://www.sourcelondon.net",
"TelephoneNo": "020 3056 8989"
},
"DeviceController": {
"OrganisationName": "Source London",
"SchemeCode": "SRC_LDN",
"Website": "https://www.sourcelondon.net",
"TelephoneNo": "020 3056 8989"
},
"DeviceAccess": [],
"DeviceNetworks": "Source London",
"ChargeDeviceStatus": "In service",
"PublishStatus": "Published",
"DeviceValidated": "0",
"RecordModerated": "Y",
"RecordLastUpdated": "2015-09-02 11:28:16",
"RecordLastUpdatedBy": "NCR Admin",
"PaymentRequiredFlag": false,
"PaymentDetails": "",
"SubscriptionRequiredFlag": true,
"SubscriptionDetails": "\u00a35 per annum for RFiD card",
"ParkingFeesFlag": false,
"ParkingFeesDetails": "",
"ParkingFeesUrl": null,
"AccessRestrictionFlag": false,
"AccessRestrictionDetails": "",
"PhysicalRestrictionFlag": false,
"PhysicalRestrictionText": "",
"OnStreetFlag": true,
"LocationType": "On-street",
"Bearing": null,
"Accessible24Hours": false
}
]
};
let myConnArr = [];
Array.from(myJson.ChargeDevice[0].Connector, conn =>
{
if(!myConnArr.includes(conn.ConnectorType)) myConnArr.push(conn.ConnectorType)
});
console.log(myConnArr.join(", \n"));
You could take a Set and check if the item is not in the set, then use the item and add this item to the set.
var array = [{ connector: ['ConnectorType1'] }, { connector: ['ConnectorType1', 'ConnectorType2', 'ConnectorType3'] }, { connector: ['ConnectorType1'] }, { connector: ['ConnectorType1', 'ConnectorType2'] }],
connectors = new Set;
array.forEach(({ connector }) => connector.forEach(c => {
if (connectors.has(c)) return;
console.log(c);
connectors.add(c);
}));
Not sure if this is what you're after, but here is a function that returns an array of the unique connectors within a ChargeDevice and a little test.
function getUniqueConnectors(data) {
var connectors = [];
for (let i in data.ChargeDevice) {
for (let x = 0; x < data.ChargeDevice[i].Connector.length; x++) {
if (connectors.indexOf(data.ChargeDevice[i].Connector[x].ConnectorType) === -1) {
connectors.push(data.ChargeDevice[i].Connector[x].ConnectorType);
}
}
}
return connectors;
}
var objectOne = {
"ChargeDevice": [
{
"ChargeDeviceId": "cfeedcdd5e287bef4b583158a12363f1",
"ChargeDeviceRef": "SRC_LDN60188",
"ChargeDeviceName": "2 Riddons Road",
"ChargeDeviceText": null,
"ChargeDeviceLocation": {
"Latitude": "51.431454",
"Longitude": "0.031175",
"Address": {
"SubBuildingName": null,
"BuildingName": "",
"BuildingNumber": "",
"Thoroughfare": "Riddons Road",
"Street": "Junction with Chinbrook Road",
"DoubleDependantLocality": null,
"DependantLocality": null,
"PostTown": "Leek",
"County": "Greater London",
"PostCode": "SE12 9QR",
"Country": "gb",
"UPRN": null
},
"LocationShortDescription": null,
"LocationLongDescription": ""
},
"ChargeDeviceManufacturer": null,
"ChargeDeviceModel": null,
"PublishStatusID": "1",
"DateCreated": "2014-08-19 05:15:02",
"DateUpdated": "2015-09-02 11:28:16",
"Attribution": "Source London",
"DateDeleted": "n/a",
"Connector": [
{
"ConnectorId": "1",
"ConnectorType": "3-pin Type G (BS1363)",
"RatedOutputkW": "3.7",
"RatedOutputVoltage": "230",
"RatedOutputCurrent": "16",
"ChargeMethod": "Single Phase AC",
"ChargeMode": "1",
"ChargePointStatus": "In service",
"TetheredCable": "0",
"Information": " x 3-pin square (BS 1363) - Standard (up to 3.7kW, 13-16A)",
"Validated": "0"
},
{
"ConnectorId": "2",
"ConnectorType": "Type 2 Mennekes (IEC62196)",
"RatedOutputkW": "7.0",
"RatedOutputVoltage": "230",
"RatedOutputCurrent": "32",
"ChargeMethod": "Single Phase AC",
"ChargeMode": "3",
"ChargePointStatus": "In service",
"TetheredCable": "0",
"Information": " x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
"Validated": "0"
},
{
"ConnectorId": "2",
"ConnectorType": "Type 2 Mennekes (IEC62196)",
"RatedOutputkW": "7.0",
"RatedOutputVoltage": "230",
"RatedOutputCurrent": "32",
"ChargeMethod": "Single Phase AC",
"ChargeMode": "3",
"ChargePointStatus": "In service",
"TetheredCable": "0",
"Information": " x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
"Validated": "0"
},
{
"ConnectorId": "2",
"ConnectorType": "Type 2 Mennekes (IEC62196)",
"RatedOutputkW": "7.0",
"RatedOutputVoltage": "230",
"RatedOutputCurrent": "32",
"ChargeMethod": "Single Phase AC",
"ChargeMode": "3",
"ChargePointStatus": "In service",
"TetheredCable": "0",
"Information": " x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
"Validated": "0"
},
],
"DeviceOwner": {
"OrganisationName": "Source London",
"SchemeCode": "SRC_LDN",
"Website": "https://www.sourcelondon.net",
"TelephoneNo": "020 3056 8989"
},
"DeviceController": {
"OrganisationName": "Source London",
"SchemeCode": "SRC_LDN",
"Website": "https://www.sourcelondon.net",
"TelephoneNo": "020 3056 8989"
},
"DeviceAccess": [],
"DeviceNetworks": "Source London",
"ChargeDeviceStatus": "In service",
"PublishStatus": "Published",
"DeviceValidated": "0",
"RecordModerated": "Y",
"RecordLastUpdated": "2015-09-02 11:28:16",
"RecordLastUpdatedBy": "NCR Admin",
"PaymentRequiredFlag": false,
"PaymentDetails": "",
"SubscriptionRequiredFlag": true,
"SubscriptionDetails": "\u00a35 per annum for RFiD card",
"ParkingFeesFlag": false,
"ParkingFeesDetails": "",
"ParkingFeesUrl": null,
"AccessRestrictionFlag": false,
"AccessRestrictionDetails": "",
"PhysicalRestrictionFlag": false,
"PhysicalRestrictionText": "",
"OnStreetFlag": true,
"LocationType": "On-street",
"Bearing": null,
"Accessible24Hours": false
},
]
};
console.log(getUniqueConnectors(objectOne)); //["3-pin Type G (BS1363)", "Type 2 Mennekes (IEC62196)"]
I have taken this script from the net and need a little push to understand how to achieve the following:
Commission Should begin increment only after #yw0 val > 200.
When under 200 commission will stay 0 but Credit Amount will change as normal.
Also for some reason if i change Comission to 0 the Total is Still 51.6 and i dont know why?
Current Code:
jQuery(function($) {
jQuery('#yw0').slider({
'range': 'max',
'min': 0,
'max': 130,
'slide': function(event, ui) {
$("#Credit_Amount").val(ui.value);
$("#AmountsDiv").text(GetAmount(ui.value));
GenerateAdditionalPrecents(ui.value);
RefreshValues();
if (parseInt($("#AmountsDiv").text()) > "200") {
$('#t2').removeClass('not-active');
} else {
$('#t2').addClass('not-active');
};
},
'value': 0
});
jQuery('#yw1').slider({
'range': 'max',
'min': 0,
'max': 5,
'slide': function(event, ui) {
$("#Credit_Duration").val(ui.value);
$("#DurationDiv").text(GetDuration(ui.value));
RefreshValues();
},
'value': 0
});
});
function GetAmount(id) {
var Amounts = new Array("50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100", "105", "110", "115", "120", "125", "130", "135", "140", "145", "150", "155", "160", "165", "170", "175", "180", "185", "190", "195", "200", "205", "210", "215", "220", "225", "230", "235", "240", "245", "250", "255", "260", "265", "270", "275", "280", "285", "290", "295", "300", "305", "310", "315", "320", "325", "330", "335", "340", "345", "350", "355", "360", "365", "370", "375", "380", "385", "390", "395", "400", "405", "410", "415", "420", "425", "430", "435", "440", "445", "450", "455", "460", "465", "470", "475", "480", "485", "490", "495", "500", "505", "510", "515", "520", "525", "530", "535", "540", "545", "550", "555", "560", "565", "570", "575", "580", "585", "590", "595", "600", "605", "610", "615", "620", "625", "630", "635", "640", "645", "650", "655", "660", "665", "670", "675", "680", "685", "690", "695", "700");
return Amounts[id];
}
function GetDuration(id) {
var Durations = new Array("5", "10", "15", "20", "25", "30");
return Durations[id];
}
function RefreshValues() {
var AmountsMatrix = new Array();
AmountsMatrix[0] = new Array("1.6", "1.76", "1.92", "2.08", "2.24", "2.4", "2.56", "2.72", "2.88", "3.04", "3.2", "3.36", "3.52", "3.68", "3.84", "4", "4.16", "4.32", "4.48", "4.64", "4.8", "4.96", "5.12", "5.28", "5.44", "5.6", "5.76", "5.92", "6.08", "6.24", "6.4", "6.56", "6.72", "6.88", "7.04", "7.2", "7.36", "7.52", "7.68", "7.84", "8", "8.16", "8.32", "8.48", "8.64", "8.8", "8.96", "9.12", "9.28", "9.44", "9.6", "9.76", "9.92", "10.08", "10.24", "10.4", "10.56", "10.72", "10.88", "11.04", "11.2", "11.36", "11.52", "11.68", "11.84", "12", "12.16", "12.32", "12.48", "12.64", "12.8", "12.96", "13.12", "13.28", "13.44", "13.6", "13.76", "13.92", "14.08", "14.24", "14.4", "14.56", "14.72", "14.88", "15.04", "15.2", "15.36", "15.52", "15.68", "15.84", "16", "16.16", "16.32", "16.48", "16.64", "16.8", "16.96", "17.12", "17.28", "17.44", "17.6", "17.76", "17.92", "18.08", "18.24", "18.4", "18.56", "18.72", "18.88", "19.04", "19.2", "19.36", "19.52", "19.68", "19.84", "20", "20.16", "20.32", "20.48", "20.64", "20.8", "20.96", "21.12", "21.28", "21.44", "21.6", "21.76", "21.92", "22.08", "22.24", "22.4");
AmountsMatrix[1] = new Array("3.15", "3.46", "3.78", "4.09", "4.41", "4.72", "5.04", "5.35", "5.67", "5.98", "6.3", "6.61", "6.93", "7.24", "7.56", "7.87", "8.19", "8.5", "8.82", "9.13", "9.45", "9.76", "10.08", "10.39", "10.71", "11.02", "11.34", "11.65", "11.97", "12.28", "12.6", "12.91", "13.23", "13.54", "13.86", "14.17", "14.49", "14.8", "15.12", "15.43", "15.75", "16.06", "16.38", "16.69", "17.01", "17.32", "17.64", "17.95", "18.27", "18.58", "18.9", "19.21", "19.53", "19.84", "20.16", "20.47", "20.79", "21.1", "21.42", "21.73", "22.05", "22.36", "22.68", "22.99", "23.31", "23.62", "23.94", "24.25", "24.57", "24.88", "25.2", "25.51", "25.83", "26.14", "26.46", "26.77", "27.09", "27.4", "27.72", "28.03", "28.35", "28.66", "28.98", "29.29", "29.61", "29.92", "30.24", "30.55", "30.87", "31.18", "31.5", "31.81", "32.13", "32.44", "32.76", "33.07", "33.39", "33.7", "34.02", "34.33", "34.65", "34.96", "35.28", "35.59", "35.91", "36.22", "36.54", "36.85", "37.17", "37.48", "37.8", "38.11", "38.43", "38.74", "39.06", "39.37", "39.69", "40", "40.32", "40.63", "40.95", "41.26", "41.58", "41.89", "42.21", "42.52", "42.84", "43.15", "43.47", "43.78", "44.1");
AmountsMatrix[2] = new Array("4.75", "5.22", "5.7", "6.17", "6.65", "7.12", "7.6", "8.07", "8.55", "9.02", "9.5", "9.97", "10.45", "10.92", "11.4", "11.87", "12.35", "12.82", "13.3", "13.77", "14.25", "14.72", "15.2", "15.67", "16.15", "16.62", "17.1", "17.57", "18.05", "18.52", "19", "19.47", "19.95", "20.42", "20.9", "21.37", "21.85", "22.32", "22.8", "23.27", "23.75", "24.22", "24.7", "25.17", "25.65", "26.12", "26.6", "27.07", "27.55", "28.02", "28.5", "28.97", "29.45", "29.92", "30.4", "30.87", "31.35", "31.82", "32.3", "32.77", "33.25", "33.72", "34.2", "34.67", "35.15", "35.62", "36.1", "36.57", "37.05", "37.52", "38", "38.47", "38.95", "39.42", "39.9", "40.37", "40.85", "41.32", "41.8", "42.27", "42.75", "43.22", "43.7", "44.17", "44.65", "45.12", "45.6", "46.07", "46.55", "47.02", "47.5", "47.97", "48.45", "48.92", "49.4", "49.87", "50.35", "50.82", "51.3", "51.77", "52.25", "52.72", "53.2", "53.67", "54.15", "54.62", "55.1", "55.57", "56.05", "56.52", "57", "57.47", "57.95", "58.42", "58.9", "59.37", "59.85", "60.32", "60.8", "61.27", "61.75", "62.22", "62.7", "63.17", "63.65", "64.12", "64.6", "65.07", "65.55", "66.02", "66.5");
AmountsMatrix[3] = new Array("6.25", "6.87", "7.5", "8.12", "8.75", "9.37", "10", "10.62", "11.25", "11.87", "12.5", "13.12", "13.75", "14.37", "15", "15.62", "16.25", "16.87", "17.5", "18.12", "18.75", "19.37", "20", "20.62", "21.25", "21.87", "22.5", "23.12", "23.75", "24.37", "25", "25.62", "26.25", "26.87", "27.5", "28.12", "28.75", "29.37", "30", "30.62", "31.25", "31.87", "32.5", "33.12", "33.75", "34.37", "35", "35.62", "36.25", "36.87", "37.5", "38.12", "38.75", "39.37", "40", "40.62", "41.25", "41.87", "42.5", "43.12", "43.75", "44.37", "45", "45.62", "46.25", "46.87", "47.5", "48.12", "48.75", "49.37", "50", "50.62", "51.25", "51.87", "52.5", "53.12", "53.75", "54.37", "55", "55.62", "56.25", "56.87", "57.5", "58.12", "58.75", "59.37", "60", "60.62", "61.25", "61.87", "62.5", "63.12", "63.75", "64.37", "65", "65.62", "66.25", "66.87", "67.5", "68.12", "68.75", "69.37", "70", "70.62", "71.25", "71.87", "72.5", "73.12", "73.75", "74.37", "75", "75.62", "76.25", "76.87", "77.5", "78.12", "78.75", "79.37", "80", "80.62", "81.25", "81.87", "82.5", "83.12", "83.75", "84.37", "85", "85.62", "86.25", "86.87", "87.5");
AmountsMatrix[4] = new Array("8", "8.8", "9.6", "10.4", "11.2", "12", "12.8", "13.6", "14.4", "15.2", "16", "16.8", "17.6", "18.4", "19.2", "20", "20.8", "21.6", "22.4", "23.2", "24", "24.8", "25.6", "26.4", "27.2", "28", "28.8", "29.6", "30.4", "31.2", "32", "32.8", "33.6", "34.4", "35.2", "36", "36.8", "37.6", "38.4", "39.2", "40", "40.8", "41.6", "42.4", "43.2", "44", "44.8", "45.6", "46.4", "47.2", "48", "48.8", "49.6", "50.4", "51.2", "52", "52.8", "53.6", "54.4", "55.2", "56", "56.8", "57.6", "58.4", "59.2", "60", "60.8", "61.6", "62.4", "63.2", "64", "64.8", "65.6", "66.4", "67.2", "68", "68.8", "69.6", "70.4", "71.2", "72", "72.8", "73.6", "74.4", "75.2", "76", "76.8", "77.6", "78.4", "79.2", "80", "80.8", "81.6", "82.4", "83.2", "84", "84.8", "85.6", "86.4", "87.2", "88", "88.8", "89.6", "90.4", "91.2", "92", "92.8", "93.6", "94.4", "95.2", "96", "96.8", "97.6", "98.4", "99.2", "100", "100.8", "101.6", "102.4", "103.2", "104", "104.8", "105.6", "106.4", "107.2", "108", "108.8", "109.6", "110.4", "111.2", "112");
AmountsMatrix[5] = new Array("9.5", "10.45", "11.4", "12.35", "13.3", "14.25", "15.2", "16.15", "17.1", "18.05", "19", "19.95", "20.9", "21.85", "22.8", "23.75", "24.7", "25.65", "26.6", "27.55", "28.5", "29.45", "30.4", "31.35", "32.3", "33.25", "34.2", "35.15", "36.1", "37.05", "38", "38.95", "39.9", "40.85", "41.8", "42.75", "43.7", "44.65", "45.6", "46.55", "47.5", "48.45", "49.4", "50.35", "51.3", "52.25", "53.2", "54.15", "55.1", "56.05", "57", "57.95", "58.9", "59.85", "60.8", "61.75", "62.7", "63.65", "64.6", "65.55", "66.5", "67.45", "68.4", "69.35", "70.3", "71.25", "72.2", "73.15", "74.1", "75.05", "76", "76.95", "77.9", "78.85", "79.8", "80.75", "81.7", "82.65", "83.6", "84.55", "85.5", "86.45", "87.4", "88.35", "89.3", "90.25", "91.2", "92.15", "93.1", "94.05", "95", "95.95", "96.9", "97.85", "98.8", "99.75", "100.7", "101.65", "102.6", "103.55", "104.5", "105.45", "106.4", "107.35", "108.3", "109.25", "110.2", "111.15", "112.1", "113.05", "114", "114.95", "115.9", "116.85", "117.8", "118.75", "119.7", "120.65", "121.6", "122.55", "123.5", "124.45", "125.4", "126.35", "127.3", "128.25", "129.2", "130.15", "131.1", "132.05", "133");
$("#PriceDiv").text(AmountsMatrix[$("#Credit_Duration").val()][$("#Credit_Amount").val()]);
$("#Amount2Div").text(GetAmount($("#Credit_Amount").val()));
$("#AllDiv").text(Number(GetAmount($("#Credit_Amount").val())) + Number(AmountsMatrix[$("#Credit_Duration").val()][$("#Credit_Amount").val()]));
now = new Date;
now.setDate(now.getDate() - 1 + Number(GetDuration($("#Credit_Duration").val())));
}
function GenerateAdditionalPrecents(Amount) {
var AdditionalAmountsMatrix = new Array();
AdditionalAmountsMatrix[0] = new Array("5", "5.5", "6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5", "11", "11.5", "12", "12.5", "13", "13.5", "14", "14.5", "15", "15.5", "16", "16.5", "17", "17.5", "18", "18.5", "19", "19.5", "20", "20.5", "21", "21.5", "22", "22.5", "23", "23.5", "24", "24.5", "25", "25.5", "26", "26.5", "27", "27.5", "28", "28.5", "29", "29.5", "30", "30.5", "31", "31.5", "32", "32.5", "33", "33.5", "34", "34.5", "35", "35.5", "36", "36.5", "37", "37.5", "38", "38.5", "39", "39.5", "40", "40.5", "41", "41.5", "42", "42.5", "43", "43.5", "44", "44.5", "45", "45.5", "46", "46.5", "47", "47.5", "48", "48.5", "49", "49.5", "50", "50.5", "51", "51.5", "52", "52.5", "53", "53.5", "54", "54.5", "55", "55.5", "56", "56.5", "57", "57.5", "58", "58.5", "59", "59.5", "60", "60.5", "61", "61.5", "62", "62.5", "63", "63.5", "64", "64.5", "65", "65.5", "66", "66.5", "67", "67.5", "68", "68.5", "69", "69.5", "70");
AdditionalAmountsMatrix[1] = new Array("6", "6.6", "7.2", "7.8", "8.4", "9", "9.6", "10.2", "10.8", "11.4", "12", "12.6", "13.2", "13.8", "14.4", "15", "15.6", "16.2", "16.8", "17.4", "18", "18.6", "19.2", "19.8", "20.4", "21", "21.6", "22.2", "22.8", "23.4", "24", "24.6", "25.2", "25.8", "26.4", "27", "27.6", "28.2", "28.8", "29.4", "30", "30.6", "31.2", "31.8", "32.4", "33", "33.6", "34.2", "34.8", "35.4", "36", "36.6", "37.2", "37.8", "38.4", "39", "39.6", "40.2", "40.8", "41.4", "42", "42.6", "43.2", "43.8", "44.4", "45", "45.6", "46.2", "46.8", "47.4", "48", "48.6", "49.2", "49.8", "50.4", "51", "51.6", "52.2", "52.8", "53.4", "54", "54.6", "55.2", "55.8", "56.4", "57", "57.6", "58.2", "58.8", "59.4", "60", "60.6", "61.2", "61.8", "62.4", "63", "63.6", "64.2", "64.8", "65.4", "66", "66.6", "67.2", "67.8", "68.4", "69", "69.6", "70.2", "70.8", "71.4", "72", "72.6", "73.2", "73.8", "74.4", "75", "75.6", "76.2", "76.8", "77.4", "78", "78.6", "79.2", "79.8", "80.4", "81", "81.6", "82.2", "82.8", "83.4", "84");
AdditionalAmountsMatrix[2] = new Array("7", "7.7", "8.4", "9.1", "9.8", "10.5", "11.2", "11.9", "12.6", "13.3", "14", "14.7", "15.4", "16.1", "16.8", "17.5", "18.2", "18.9", "19.6", "20.3", "21", "21.7", "22.4", "23.1", "23.8", "24.5", "25.2", "25.9", "26.6", "27.3", "28", "28.7", "29.4", "30.1", "30.8", "31.5", "32.2", "32.9", "33.6", "34.3", "35", "35.7", "36.4", "37.1", "37.8", "38.5", "39.2", "39.9", "40.6", "41.3", "42", "42.7", "43.4", "44.1", "44.8", "45.5", "46.2", "46.9", "47.6", "48.3", "49", "49.7", "50.4", "51.1", "51.8", "52.5", "53.2", "53.9", "54.6", "55.3", "56", "56.7", "57.4", "58.1", "58.8", "59.5", "60.2", "60.9", "61.6", "62.3", "63", "63.7", "64.4", "65.1", "65.8", "66.5", "67.2", "67.9", "68.6", "69.3", "70", "70.7", "71.4", "72.1", "72.8", "73.5", "74.2", "74.9", "75.6", "76.3", "77", "77.7", "78.4", "79.1", "79.8", "80.5", "81.2", "81.9", "82.6", "83.3", "84", "84.7", "85.4", "86.1", "86.8", "87.5", "88.2", "88.9", "89.6", "90.3", "91", "91.7", "92.4", "93.1", "93.8", "94.5", "95.2", "95.9", "96.6", "97.3", "98");
AdditionalAmountsMatrix[3] = new Array("8", "8.8", "9.6", "10.4", "11.2", "12", "12.8", "13.6", "14.4", "15.2", "16", "16.8", "17.6", "18.4", "19.2", "20", "20.8", "21.6", "22.4", "23.2", "24", "24.8", "25.6", "26.4", "27.2", "28", "28.8", "29.6", "30.4", "31.2", "32", "32.8", "33.6", "34.4", "35.2", "36", "36.8", "37.6", "38.4", "39.2", "40", "40.8", "41.6", "42.4", "43.2", "44", "44.8", "45.6", "46.4", "47.2", "48", "48.8", "49.6", "50.4", "51.2", "52", "52.8", "53.6", "54.4", "55.2", "56", "56.8", "57.6", "58.4", "59.2", "60", "60.8", "61.6", "62.4", "63.2", "64", "64.8", "65.6", "66.4", "67.2", "68", "68.8", "69.6", "70.4", "71.2", "72", "72.8", "73.6", "74.4", "75.2", "76", "76.8", "77.6", "78.4", "79.2", "80", "80.8", "81.6", "82.4", "83.2", "84", "84.8", "85.6", "86.4", "87.2", "88", "88.8", "89.6", "90.4", "91.2", "92", "92.8", "93.6", "94.4", "95.2", "96", "96.8", "97.6", "98.4", "99.2", "100", "100.8", "101.6", "102.4", "103.2", "104", "104.8", "105.6", "106.4", "107.2", "108", "108.8", "109.6", "110.4", "111.2", "112");
AdditionalAmountsMatrix[4] = new Array("9.5", "10.45", "11.4", "12.35", "13.3", "14.25", "15.2", "16.15", "17.1", "18.05", "19", "19.95", "20.9", "21.85", "22.8", "23.75", "24.7", "25.65", "26.6", "27.55", "28.5", "29.45", "30.4", "31.35", "32.3", "33.25", "34.2", "35.15", "36.1", "37.05", "38", "38.95", "39.9", "40.85", "41.8", "42.75", "43.7", "44.65", "45.6", "46.55", "47.5", "48.45", "49.4", "50.35", "51.3", "52.25", "53.2", "54.15", "55.1", "56.05", "57", "57.95", "58.9", "59.85", "60.8", "61.75", "62.7", "63.65", "64.6", "65.55", "66.5", "67.45", "68.4", "69.35", "70.3", "71.25", "72.2", "73.15", "74.1", "75.05", "76", "76.95", "77.9", "78.85", "79.8", "80.75", "81.7", "82.65", "83.6", "84.55", "85.5", "86.45", "87.4", "88.35", "89.3", "90.25", "91.2", "92.15", "93.1", "94.05", "95", "95.95", "96.9", "97.85", "98.8", "99.75", "100.7", "101.65", "102.6", "103.55", "104.5", "105.45", "106.4", "107.35", "108.3", "109.25", "110.2", "111.15", "112.1", "113.05", "114", "114.95", "115.9", "116.85", "117.8", "118.75", "119.7", "120.65", "121.6", "122.55", "123.5", "124.45", "125.4", "126.35", "127.3", "128.25", "129.2", "130.15", "131.1", "132.05", "133");
AdditionalAmountsMatrix[5] = new Array("11", "12.1", "13.2", "14.3", "15.4", "16.5", "17.6", "18.7", "19.8", "20.9", "22", "23.1", "24.2", "25.3", "26.4", "27.5", "28.6", "29.7", "30.8", "31.9", "33", "34.1", "35.2", "36.3", "37.4", "38.5", "39.6", "40.7", "41.8", "42.9", "44", "45.1", "46.2", "47.3", "48.4", "49.5", "50.6", "51.7", "52.8", "53.9", "55", "56.1", "57.2", "58.3", "59.4", "60.5", "61.6", "62.7", "63.8", "64.9", "66", "67.1", "68.2", "69.3", "70.4", "71.5", "72.6", "73.7", "74.8", "75.9", "77", "78.1", "79.2", "80.3", "81.4", "82.5", "83.6", "84.7", "85.8", "86.9", "88", "89.1", "90.2", "91.3", "92.4", "93.5", "94.6", "95.7", "96.8", "97.9", "99", "100.1", "101.2", "102.3", "103.4", "104.5", "105.6", "106.7", "107.8", "108.9", "110", "111.1", "112.2", "113.3", "114.4", "115.5", "116.6", "117.7", "118.8", "119.9", "121", "122.1", "123.2", "124.3", "125.4", "126.5", "127.6", "128.7", "129.8", "130.9", "132", "133.1", "134.2", "135.3", "136.4", "137.5", "138.6", "139.7", "140.8", "141.9", "143", "144.1", "145.2", "146.3", "147.4", "148.5", "149.6", "150.7", "151.8", "152.9", "154");
$("#AdditionalPrecentDiv0").text(AdditionalAmountsMatrix[0][Amount]);
$("#AdditionalPrecentDiv1").text(AdditionalAmountsMatrix[1][Amount]);
$("#AdditionalPrecentDiv2").text(AdditionalAmountsMatrix[2][Amount]);
$("#AdditionalPrecentDiv3").text(AdditionalAmountsMatrix[3][Amount]);
$("#AdditionalPrecentDiv4").text(AdditionalAmountsMatrix[4][Amount]);
$("#AdditionalPrecentDiv5").text(AdditionalAmountsMatrix[5][Amount]);
}
#yw0,
#yw1,
#yw2,
#yw3 {
width: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<table class="pull-right" style="border:1px solid;float:right;width:170px;">
<tr>
<td>Credit Amount:</td>
<td id="Amount2Div">50</td>
</tr>
<tr>
<td>Commission:</td>
<td id="PriceDiv">0</td>
</tr>
<tr>
<td>Total:</td>
<td id="AllDiv">51.6</td>
</tr>
</table>
<div class="calculator">
<div class="slider_wrapper">
<div id="yw0"></div>
</div>
<div class="value" id="AmountsDiv">50
<input value="0" size="1" readonly="readonly" name="Credit[Amount]" id="Credit_Amount" type="hidden" />
</div>
<div class="slider_wrapper2">
<div id="yw1"></div>
</div>
<div class="value2" id="DurationDiv">5</div>
<input value="0" size="1" readonly="readonly" name="Credit[Amount]" id="Credit_Amount" type="hidden" />
<input value="0" size="1" readonly="readonly" name="Credit[Amount]" id="Credit_Amount2nd" type="hidden" />
<input value="0" size="1" readonly="readonly" name="Credit[Duration]" id="Credit_Duration" type="hidden" />
</div>
A Possible Theory i had was to :
jQuery('#yw0').slider({
'range': 'max',
'min': 0,
'max': 130,
'slide': function(event, ui) {
$("#AmountsDiv").text(GetAmount(ui.value));
GenerateAdditionalPrecents(ui.value);
RefreshValues();
var vareset = 0;
if (parseInt($("#AmountsDiv").text()) > "200") {
$('#t2').removeClass('not-active');
if (vareset == 0) {
ui.value = 0;
vareset = 1;
};
$("#Credit_Amount2nd").val(ui.value);
} else {
$('#t2').addClass('not-active');
$("#Credit_Amount").val(ui.value);
};
},
'value': 0
});
After trying though, the value of Credit_amount2nd just changes to 0 and sticks there not incrementing anymore?
I want to get domain name only using javascript. Ex
vn.search.yahoo.com -> yahoo
vn.search.yahoo.com.vn -> yahoo
sub1.sub2.sub3.abcdef.co.uk -> abcdef
Thank you!
Edit: "domain" = domain without extension (ex: .com, .net, .co.uk...) and without sub domain (ex: www, email, cdn, support...)
Use location.host and cut off subdomains and the TLD:
var domain = (location.host.match(/([^.]+)\.\w{2,3}(?:\.\w{2})?$/) || [])[1]
update: as #demix pointed out, this fails for 2 and 3-letter domains. It also won't work for domains like aero, jobs and dozens others.
The only way around is to know valid TLDs in advance, so here is a more appropriate function:
// http://data.iana.org/TLD/tlds-alpha-by-domain.txt
var TLDs = ["ac", "ad", "ae", "aero", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar", "arpa", "as", "asia", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "biz", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cat", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "com", "coop", "cr", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "edu", "ee", "eg", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gov", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "info", "int", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jobs", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mil", "mk", "ml", "mm", "mn", "mo", "mobi", "mp", "mq", "mr", "ms", "mt", "mu", "museum", "mv", "mw", "mx", "my", "mz", "na", "name", "nc", "ne", "net", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "org", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "pro", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "su", "sv", "sy", "sz", "tc", "td", "tel", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tp", "tr", "travel", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "xn--0zwm56d", "xn--11b5bs3a9aj6g", "xn--3e0b707e", "xn--45brj9c", "xn--80akhbyknj4f", "xn--90a3ac", "xn--9t4b11yi5a", "xn--clchc0ea0b2g2a9gcd", "xn--deba0ad", "xn--fiqs8s", "xn--fiqz9s", "xn--fpcrj9c3d", "xn--fzc2c9e2c", "xn--g6w251d", "xn--gecrj9c", "xn--h2brj9c", "xn--hgbk6aj7f53bba", "xn--hlcj6aya9esc7a", "xn--j6w193g", "xn--jxalpdlp", "xn--kgbechtv", "xn--kprw13d", "xn--kpry57d", "xn--lgbbat1ad8j", "xn--mgbaam7a8h", "xn--mgbayh7gpa", "xn--mgbbh1a71e", "xn--mgbc0a9azcg", "xn--mgberp4a5d4ar", "xn--o3cw4h", "xn--ogbpf8fl", "xn--p1ai", "xn--pgbs0dh", "xn--s9brj9c", "xn--wgbh1c", "xn--wgbl6a", "xn--xkc2al3hye2a", "xn--xkc2dl3a5ee0h", "xn--yfro4i67o", "xn--ygbi2ammx", "xn--zckzah", "xxx", "ye", "yt", "za", "zm", "zw"].join()
function getDomain(url){
var parts = url.split('.');
if (parts[0] === 'www' && parts[1] !== 'com'){
parts.shift()
}
var ln = parts.length
, i = ln
, minLength = parts[parts.length-1].length
, part
// iterate backwards
while(part = parts[--i]){
// stop when we find a non-TLD part
if (i === 0 // 'asia.com' (last remaining must be the SLD)
|| i < ln-2 // TLDs only span 2 levels
|| part.length < minLength // 'www.cn.com' (valid TLD as second-level domain)
|| TLDs.indexOf(part) < 0 // officialy not a TLD
){
return part
}
}
}
getDomain(location.host)
I hope I didn't miss too many corner cases. This should be available in the location object :(
Test cases: http://jsfiddle.net/hqBKd/4/
A list of TLDs can be found here: http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1
I was looking for something that would work for the majority of cases, without having to maintain the TLD list (and skip it's size!). It seems to me that you can do this pretty accurately by looking instead at the Second-Level Domain for common ones:
function getDomainName(domain) {
var parts = domain.split('.').reverse();
var cnt = parts.length;
if (cnt >= 3) {
// see if the second level domain is a common SLD.
if (parts[1].match(/^(com|edu|gov|net|mil|org|nom|co|name|info|biz)$/i)) {
return parts[2] + '.' + parts[1] + '.' + parts[0];
}
}
return parts[1]+'.'+parts[0];
}
Fiddle & Tests # http://jsfiddle.net/mZPaf/2/
Critiques/thoughts welcome.
var docdomain = document.domain.split('.');
var dom1 = "";
if (typeof (docdomain[docdomain.length - 2]) != 'undefined') dom1 = docdomain[docdomain.length - 2] + '.';
var domain = dom1 + docdomain[docdomain.length - 1];
console.log(domain);
//without subdomains
The only way I can imagine is list all the TLD. Sample code like below.
function getDomainName(){
var domainList = ['com','org','net',...];//all TLD
var tokens = document.domain.split('.');
while(tokens.length){
var token = tokens.pop();
if( domainList.indexOf(token) == -1 ){
return token;
}
}
return null;
}
Array.prototype.indexOf should do some fix in IE.
Without having a complete list of TLD's (which would get very long). If you just need the domain name from the current page you can use my technique (using cookies to find the root domain)
Javascript - Get Domain Name Excluding Subdomain
To remove the extension you can then use the first element from a str.split('.')[0]
It's simple:
var tokens = document.domain.split('.');
var domain = tokens[tokens.length - 2];
i needed to do this and whipped up something simple that accounted for my use case
function stripSubDomainAndTLD (domain) {
return domain.replace(/^(?:[a-z0-9\-\.]+\.)??([a-z0-9\-]+)(?:\.com|\.net|\.org|\.biz|\.ws|\.in|\.me|\.co\.uk|\.co|\.org\.uk|\.ltd\.uk|\.plc\.uk|\.me\.uk|\.edu|\.mil|\.br\.com|\.cn\.com|\.eu\.com|\.hu\.com|\.no\.com|\.qc\.com|\.sa\.com|\.se\.com|\.se\.net|\.us\.com|\.uy\.com|\.ac|\.co\.ac|\.gv\.ac|\.or\.ac|\.ac\.ac|\.af|\.am|\.as|\.at|\.ac\.at|\.co\.at|\.gv\.at|\.or\.at|\.asn\.au|\.com\.au|\.edu\.au|\.org\.au|\.net\.au|\.id\.au|\.be|\.ac\.be|\.adm\.br|\.adv\.br|\.am\.br|\.arq\.br|\.art\.br|\.bio\.br|\.cng\.br|\.cnt\.br|\.com\.br|\.ecn\.br|\.eng\.br|\.esp\.br|\.etc\.br|\.eti\.br|\.fm\.br|\.fot\.br|\.fst\.br|\.g12\.br|\.gov\.br|\.ind\.br|\.inf\.br|\.jor\.br|\.lel\.br|\.med\.br|\.mil\.br|\.net\.br|\.nom\.br|\.ntr\.br|\.odo\.br|\.org\.br|\.ppg\.br|\.pro\.br|\.psc\.br|\.psi\.br|\.rec\.br|\.slg\.br|\.tmp\.br|\.tur\.br|\.tv\.br|\.vet\.br|\.zlg\.br|\.br|\.ab\.ca|\.bc\.ca|\.mb\.ca|\.nb\.ca|\.nf\.ca|\.ns\.ca|\.nt\.ca|\.on\.ca|\.pe\.ca|\.qc\.ca|\.sk\.ca|\.yk\.ca|\.ca|\.cc|\.ac\.cn|\.com\.cn|\.edu\.cn|\.gov\.cn|\.org\.cn|\.bj\.cn|\.sh\.cn|\.tj\.cn|\.cq\.cn|\.he\.cn|\.nm\.cn|\.ln\.cn|\.jl\.cn|\.hl\.cn|\.js\.cn|\.zj\.cn|\.ah\.cn|\.gd\.cn|\.gx\.cn|\.hi\.cn|\.sc\.cn|\.gz\.cn|\.yn\.cn|\.xz\.cn|\.sn\.cn|\.gs\.cn|\.qh\.cn|\.nx\.cn|\.xj\.cn|\.tw\.cn|\.hk\.cn|\.mo\.cn|\.cn|\.cx|\.cz|\.de|\.dk|\.fo|\.com\.ec|\.tm\.fr|\.com\.fr|\.asso\.fr|\.presse\.fr|\.fr|\.gf|\.gs|\.co\.il|\.net\.il|\.ac\.il|\.k12\.il|\.gov\.il|\.muni\.il|\.ac\.in|\.co\.in|\.org\.in|\.ernet\.in|\.gov\.in|\.net\.in|\.res\.in|\.is|\.it|\.ac\.jp|\.co\.jp|\.go\.jp|\.or\.jp|\.ne\.jp|\.ac\.kr|\.co\.kr|\.go\.kr|\.ne\.kr|\.nm\.kr|\.or\.kr|\.li|\.lt|\.lu|\.asso\.mc|\.tm\.mc|\.com\.mm|\.org\.mm|\.net\.mm|\.edu\.mm|\.gov\.mm|\.ms|\.nl|\.no|\.nu|\.pl|\.ro|\.org\.ro|\.store\.ro|\.tm\.ro|\.firm\.ro|\.www\.ro|\.arts\.ro|\.rec\.ro|\.info\.ro|\.nom\.ro|\.nt\.ro|\.se|\.si|\.com\.sg|\.org\.sg|\.net\.sg|\.gov\.sg|\.sk|\.st|\.tf|\.ac\.th|\.co\.th|\.go\.th|\.mi\.th|\.net\.th|\.or\.th|\.tm|\.to|\.com\.tr|\.edu\.tr|\.gov\.tr|\.k12\.tr|\.net\.tr|\.org\.tr|\.com\.tw|\.org\.tw|\.net\.tw|\.ac\.uk|\.uk\.com|\.uk\.net|\.gb\.com|\.gb\.net|\.vg|\.sh|\.kz|\.ch|\.info|\.ua|\.gov|\.name|\.pro|\.ie|\.hk|\.com\.hk|\.org\.hk|\.net\.hk|\.edu\.hk|\.us|\.tk|\.cd|\.by|\.ad|\.lv|\.eu\.lv|\.bz|\.es|\.jp|\.cl|\.ag|\.mobi|\.eu|\.co\.nz|\.org\.nz|\.net\.nz|\.maori\.nz|\.iwi\.nz|\.io|\.la|\.md|\.sc|\.sg|\.vc|\.tw|\.travel|\.my|\.se|\.tv|\.pt|\.com\.pt|\.edu\.pt|\.asia|\.fi|\.com\.ve|\.net\.ve|\.fi|\.org\.ve|\.web\.ve|\.info\.ve|\.co\.ve|\.tel|\.im|\.gr|\.ru|\.net\.ru|\.org\.ru|\.hr|\.com\.hr)$/, '$1');
}
mainly i just wanted to remove all subdomains, unfortunately this isn't 100% for some of the new TLD's but it works pretty well, and you can always add to the regex.
http://jsfiddle.net/icodeforlove/TzjJE/2/
With the help of other friend's code examples given above I created a function which will return only domain name and if it is not valid domain for example TLD is missing then it will attach ".com" as per my requirement.
function getDomain(url){
var TLDs = ["ac", "ad", "ae", "aero", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar", "arpa", "as", "asia", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "biz", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cat", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "com", "coop", "cr", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "edu", "ee", "eg", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gov", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "info", "int", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jobs", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mil", "mk", "ml", "mm", "mn", "mo", "mobi", "mp", "mq", "mr", "ms", "mt", "mu", "museum", "mv", "mw", "mx", "my", "mz", "na", "name", "nc", "ne", "net", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "org", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "pro", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "su", "sv", "sy", "sz", "tc", "td", "tel", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tp", "tr", "travel", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "xn--0zwm56d", "xn--11b5bs3a9aj6g", "xn--3e0b707e", "xn--45brj9c", "xn--80akhbyknj4f", "xn--90a3ac", "xn--9t4b11yi5a", "xn--clchc0ea0b2g2a9gcd", "xn--deba0ad", "xn--fiqs8s", "xn--fiqz9s", "xn--fpcrj9c3d", "xn--fzc2c9e2c", "xn--g6w251d", "xn--gecrj9c", "xn--h2brj9c", "xn--hgbk6aj7f53bba", "xn--hlcj6aya9esc7a", "xn--j6w193g", "xn--jxalpdlp", "xn--kgbechtv", "xn--kprw13d", "xn--kpry57d", "xn--lgbbat1ad8j", "xn--mgbaam7a8h", "xn--mgbayh7gpa", "xn--mgbbh1a71e", "xn--mgbc0a9azcg", "xn--mgberp4a5d4ar", "xn--o3cw4h", "xn--ogbpf8fl", "xn--p1ai", "xn--pgbs0dh", "xn--s9brj9c", "xn--wgbh1c", "xn--wgbl6a", "xn--xkc2al3hye2a", "xn--xkc2dl3a5ee0h", "xn--yfro4i67o", "xn--ygbi2ammx", "xn--zckzah", "xxx", "ye", "yt", "za", "zm", "zw"].join()
url = url.replace(/.*?:\/\//g, "");
url = url.replace(/www./g, "");
var parts = url.split('/');
url = parts[0];
var parts = url.split('.');
if (parts[0] === 'www' && parts[1] !== 'com'){
parts.shift()
}
var ln = parts.length
, i = ln
, minLength = parts[parts.length-1].length
, part
// iterate backwards
while(part = parts[--i]){
// stop when we find a non-TLD part
if (i === 0 // 'asia.com' (last remaining must be the SLD)
|| i < ln-2 // TLDs only span 2 levels
|| part.length < minLength // 'www.cn.com' (valid TLD as second-level domain)
|| TLDs.indexOf(part) < 0 // officialy not a TLD
){
var actual_domain = part;
break;
//return part
}
}
//console.log(actual_domain);
var tid ;
if(typeof parts[ln-1] != 'undefined' && TLDs.indexOf(parts[ln-1]) >= 0)
{
tid = '.'+parts[ln-1];
}
if(typeof parts[ln-2] != 'undefined' && TLDs.indexOf(parts[ln-2]) >= 0)
{
tid = '.'+parts[ln-2]+tid;
}
if(typeof tid != 'undefined')
actual_domain = actual_domain+tid;
else
actual_domain = actual_domain+'.com';
return actual_domain;
}
You could use document.domain to determine the domain name of the current page.
When setting document.domain, an error is thrown if the new value is invalid. A plain eTLD is invalid, while an eTLD+1 is valid. The function below loops to find a valid value, then returns the domain name portion.
This is comprehensive, because the browser uses the Public Suffix List to validate new values.
function getDomainName() {
const original = document.domain;
const parts = location.hostname.split('.');
let etld = parts.pop();
while (parts.length) {
const name = parts.pop();
const test = name + '.' + etld;
try {
document.domain = test;
// we found the eTLD+1
// reset before returning
document.domain = original;
return name;
} catch (e) {
// eTLDs and eTLD fragments fail
etld = test;
}
}
}
What about this?
function getDomain(){
if(document.domain.length){
var parts = document.domain.replace(/^(www\.)/,"").split('.');
//is there a subdomain?
while(parts.length > 2){
//removing it from our array
var subdomain = parts.shift();
}
//getting the remaining 2 elements
var domain = parts.join('.');
return domain.replace(/(^\.*)|(\.*$)/g, "");
}
return '';
}
RegEx I use to get domain name only: ([^.]*[.]){0,}([^.]*)(\.[^.]*)
The domain can be found in the second part.
function getDomainName( hostname ) {
var TLDs = new RegExp(/\.(com|net|org|biz|ltd|plc|edu|mil|asn|adm|adv|arq|art|bio|cng|cnt|ecn|eng|esp|etc|eti|fot|fst|g12|ind|inf|jor|lel|med|nom|ntr|odo|ppg|pro|psc|psi|rec|slg|tmp|tur|vet|zlg|asso|presse|k12|gov|muni|ernet|res|store|firm|arts|info|mobi|maori|iwi|travel|asia|web|tel)(\.[a-z]{2,3})?$|(\.[^\.]{2,3})(\.[^\.]{2,3})$|(\.[^\.]{2})$/);
return hostname.replace(TLDs, '').split('.').pop();
}
/* TEST */
var domains = [
'domain.com',
'subdomain.domain.com',
'www.subdomain.domain.com',
'www.subdomain.domain.info',
'www.subdomain.domain.info.xx',
'mail.subdomain.domain.co.uk',
'mail.subdomain.domain.xxx.yy',
'mail.subdomain.domain.xx.yyy',
'mail.subdomain.domain.xx',
'domain.xx'
];
var result = [];
for (var i = 0; i < domains.length; i++) {
result.push( getDomainName( domains[i] ) );
}
alert ( result.join(' | ') );
// result: domain | domain | domain | domain | domain | domain | domain | domain | domain | domain