Jump to content

In need of simply JS script


Skemcin
 Share

Recommended Posts

Hi,Can someone take a minute or two to write a simple script that checks the URL for a query string parameter "xxxx" and if that exists and the value is "zzzz" to redirect to "http://youdomain.com"I just wasted a half hour on google and I'm not finding anything and my attempts have failed at writing one from scratch - my brain is fried on this for some crazy reason.Thanks in advance.P.S. will be used inline or onload

Link to comment
Share on other sites

This should work:

<html>  <head>	<title>Querystring test</title>	<script type="text/javascript">	function test_qs()	{	  var qs = window.location.search;	  alert(qs);	  if (qs.length > 0)	  {		if (qs.charAt(0) == "?")		  qs = qs.substr(1);		chunks = qs.split("&");		for (i = 0; i < chunks.length; i++)		{		  alert("checking " + chunks[i]);		  kv = chunks[i].split("=");		  if (kv[0] == "xxxx" && kv[1] == "zzzz")		  {			//window.location.href = "http://domain.com";			alert("match");			return true;		  }		}	  }	  return false;	}	</script>  </head>  <body onload="test_qs()">	bah  </body></html>

Link to comment
Share on other sites

This will work too

<html><head>	<title>qs</title>	<script>		Array.prototype.each = function(fn) {			var item;			for(var i=0;i<this.length;i++) {				item = this[i];				fn(item,i);			}		};		var request = {			get:{}		}		var _RequestGet = window.location.href.split('?')[1];		var _RequestPairs = (_RequestGet != null) ? _RequestGet.split('&') : [];		_RequestPairs.each(function(item) {			var _RequestPair = item.split('=');			if(_RequestPair[1] != null){_RequestPair[1] = decodeURI(_RequestPair[1]);}			else{_RequestPair[1] = "";}			request.get[_RequestPair[0]] = _RequestPair[1];		});		if(request.get["xxxx"] == "zzzz") {			window.location = "http://www.yourdomain.com";		}	</script></head><body></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...