javascript - get only part of domain from document.domain -


if x.y.z.com document.domain, how z.com out of in java script? > should use regexp? please suggest. in advance! have set below in sample java script function. cannot hard code it.

<html> <head> <title>domain</title> <script type="text/javascript">  function test(){     domain="z.com"; //set domain here } </script> </head> <body bgcolor="#e7e7e7" onload="test()"> </body> 

java script code

use construction:

document.location.host.split('.').splice(-2, 2).join('.')   

upd
example use document.location.host = 'new2.new.mail.domain.com'
function split('.') splits document.location.host . array = ['new2', 'new', 'mail', 'domain', 'com']
function splice(-2, 2) deletes 2 elements end of previous array , return array = ['domain', 'com']
last join('.') function joins . prevoius array string = domain.com
profit!

upd 2
first of see reference array.splce() method

var fruits = ["banana", "orange", "apple", "mango"]; 

first argument in splice function position delete elements, in example -2 means second element end of array.
second argument in splice function - how many elements delete, in code 2 means delete 2 elements.
in second example first arguments in splice means index of element want delete elements. in javascript array indexes begins 0 , fruits[2] apple.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -