Home > Programming, Servers & Scripts >

Extract domain name from passed PPV URL (6)


11-21-2014 04:00 PM #1 ploppythejailer (Member)
Extract domain name from passed PPV URL

Hoping someone can help me with a bit of script..

Im looking to display the keyword(domain) on my lander via Voluum, ive got the following script that works, but as this is for ppv i need to clean up the domain as im using variations and just need the domain name minus any www. & .com etc.

ie my keywords maybe

amazon.com
amazon.co
www.amazon.com
ww.amazon.co
w.amazon.c
ww.amazon
etc.

So basically i just wanna return "amazon"

Heres what ive got...

Code:
<head>
<script>
function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] || ''
    );
}
</script>
</head>
<body>
<h1>Welcome to the <script>document.write(getURLParameter('keyword'))</script> quiz</h1>
</body>
anyone help ?


11-21-2014 06:36 PM #2 mykeyfocus (Member)

This should help you out buddy
http://stackoverflow.com/questions/1...st-without-www
Actually it may not help, re-read thread after replying. Id look into splitting the domain with javascript at each fullstop. In php youd use "explode" not sure with js off top of my head.


11-21-2014 09:26 PM #3 craigm (Veteran Member)

Unsure what you're looking for to be honest, do you just want to take the URL from the address bar and display just the domain with the TLD and any sub domains?

If so this rudimentary solution should work for most domains, I haven't tested it out properly so use at your own discretion

Code:
<html>
	<head>
		<script>
			function split(){

				var url = window.location.href;
				var sp = url.split(".");

				if(sp[sp.length-2].length <= 2){
					return sp[sp.length-3];
				}
				else{
					return sp[sp.length-2];
				}
			}
		</script>
	</head>

	<body>
		<h1>Welcome to the <script>document.write(split())</script> quiz</h1>
	</body>
</html>


11-22-2014 11:25 AM #4 craigm (Veteran Member)

Read it wrong, just realised you want to clean up a URL parameter,this should do the trick for most domains

Code:
<html>
	<head>
		<script>
		
			function getURLParameter(name) {
				var kw = decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] || '');
				var sp = kw.split(".");
				
				if(sp[sp.length-2].length <= 2){
					return sp[sp.length-3];
				}
				else{
					return sp[sp.length-2];
				}
			}
		</script>
	</head>

	<body>
		<h1>Welcome to the <script>document.write(getURLParameter('keyword'))</script> quiz</h1>
	</body>
</html>


11-23-2014 06:13 AM #5 zeno (Administrator)

Nice! Edited title and moved to useful scripts :-)


11-23-2014 05:43 PM #6 ploppythejailer (Member)

thanks Craig, that worked perectly.


Home > Programming, Servers & Scripts >