8 Useful jQuery Snippets For URL’s & Querystrings « Design Chemical – jQuery, WordPress, Tutorials & Plugins

In any web project you will inevitably come across a situation where being able to extract information or use the current page URL would be useful. If server-side code is not available to help we can use javascript to get this information.

The following are examples of useful code snippets that can be used – some of the examples use standard javascript functions and are not specific to jQuery:


// 1- Retrieve current URL
var url = document.URL;


// 2- Retrieve root URL
var root = location.protocol + '//' + location.host;


// 3- Get # parameter
var param = document.URL.split('#')[1];


// 4- update browser address bar URL
$('a.demo-link').click(function(){
var hash = $(this).attr('href');
location.hash = hash;
});


// 5- Redirect - insert required URL
window.location.href = "http://designchemical.com/";


// 6- Get Querystring Parameters
var vars = [], hash;
var q = document.URL.split('?')[1];
if(q != undefined){
q = q.split('&');
for(var i = 0; i < q.length; i++){
hash = q[i].split('=');
vars.push(hash[1]);
vars[hash[0]] = hash[1];
}
}
// To use -- Will alert the value of parameter a
alert(vars['a']);


// 7- Highlight Current Menu Item
var url = document.URL;
$('#menu a[href="'+url+'"]').addClass('active');


// 8- Check If Link Contains External URL
var root = location.protocol + '//' + location.host;
$('a').not(':contains(root)').click(function(){
this.target = "_blank";
});

vía 8 Useful jQuery Snippets For URL’s & Querystrings « Design Chemical – jQuery, WordPress, Tutorials & Plugins.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s