Zend Framework Style Javascript Redirect

blog has moved

I’ve moved my blog under my personal domain:

http://bsagols.com/blog

I didn’t do the proper redirection ’cause it costs 12 $ a year and I don’t see the point paying for this (12 $ (a year !) to put 1 line in an .htaccess file ?!).

Here is the new URL of this post:

bsagols / blog - Zend Framework Style Javascript Redirect

Here follows the original post…

- -

I wrote a simple javascript function that’s building a default Zend Framework URL and redirect the document to it.

My concern was to be able to send parameters through a javascript redirection to the Zend Framework. Basically instead of appending the parameters in the default way (&param1=value1&param2=…), it builds a compliant Zend Framework URL:

www.example.com/controller/action/param1/value1/param2/value2

Here is the snippet:

function redirect(url, key, value)
{
	if(typeof(key) === 'undefined' && typeof(value) === 'undefined')
	{
		document.location = url;
	}
	else
	{
		/*
		 * Example:
		 * 		- url = www.example.com/key1/value1/key2/value2/
		 * 		- key = "key1"
		 * 		- value = "toto"
		 */
		if(key !== null)
		{
			// index = 15
			var index = url.indexOf('/'+key+'/');

			var url1;
			// case 1: key is already present in URL
			if(index !== -1){

				// iafter = 21 = 15 + 4 ("key1".length) + 2 ("//".length)
				var iafter = index+key.length+2;

				// url1 = www.example.com/key1/
				url1 = url.substr(0, iafter);

				// index2 = 27 = 21 (iafter) + 6 ("value1".length)
				var index2 = url.indexOf('/', iafter);
				if(index2 !== -1){
					// url2 = /key2/value2
					var url2 = url.substring(index2, url.length);
				}
			}
			// case 2: key is not present in URL
			else{
				// url1 = www.example.com/key3/ (let's say key = "key3"...)
				url1 = url + '/'+key+'/';
			}

			// case 1: url = www.example.com/key1/
			// case 2: url = www.example.com/key3/
			url = url1;
		}
		else
		{
			// if there is no "/" at the end of url, add it
			if(url.lastIndexOf('/') != (url.length-1))
				{url += '/';}
		}

		// case 1: url = www.example.com/key1/toto
		// case 2: url = www.example.com/key3/toto
		// case key == null: url = www.example.com/toto
		url += value;
		if(url2){
			// only case 1: url = www.example.com/key1/toto/key2/value2
			url += url2;
		}

		// redirect
		document.location = url;
	}
}

Examples using it:

redirect("www.example.com");

redirect("www.example.com/controller1/action1", "param1", "value1");
// -> www.example.com/controller1/action1/param1/value1

redirect("www.example.com/controller1/action1/param1/value1/param2/value2/", "param1", "newvalue1");
// -> www.example.com/controller1/action1/param1/newvalue1/param2/value2</pre>

A precision, it just covers my needs, you may want to improve it for example passing more than 1 parameter to append / replace…

Hope this kind of help somehow.

Advertisement
Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.