2011-08-23 16 views

Respuesta

21

basta con modificar el href con el nuevo href y ya está.

$('.myLink').click(function() { 
    $(this).attr("href", this.href + "?param=1"); 
}); 
0

Simplemente configure window.location en su clic y devuelva false para evitar el comportamiento predeterminado.

Un ejemplo:

<a class="myLink" href='http://www.website.it/'>Link</a> 

<script type="text/javascript"> 
    var stringToSend = ""; 
    $('.myLink').click(function() { 
     window.location.href = this.href + "?param=" + stringToSend; 
     return false; 
    }); 
</script> 
1

Al hacer clic, se podría detener la URL actual, y vaya a otra:

var stringToSend = ""; 
$('.myLink').click(function() { 
    stringToSend="?param=1"; 
    window.location.href = $(this).attr('href') + stringToSend; // navigate to new URL 
    return false; // abort navigation to URL from <a> 
}); 
+1

+1 me adelantó :) creo que la variable "stringToSend" es redundante, aunque –

+0

@Richard H: Usted es correcta . Si se trata de una cadena estática, también se puede poner en línea. – pimvdb

+0

Muchos pueblos sugieren que window.location.href. Voy a evitar esto. un href está hecho para este uso, ¿por qué usar la ubicación href? ShankarSangoli es la mejor manera de ... – markzzz

2

También debe evitar que el comportamiento por defecto de esta manera:

var stringToSend = ""; 

// Use this to actually navigate to the changed Uri 
$('.myLink').click(function(event) { 
    event.preventDefault(); 
    stringToSend = "?param=1"; 
    window.location.href = $(this).attr('href') + stringToSend; 
}); 

// Use this just to change the Href, without navigating to uri 
$('.myLink').click(function(event) { 
    event.preventDefault(); 
    stringToSend = "?param=1"; 
    var newUri = $(this).attr('href') + stringToSend; 
    $(this).attr("href", newUri); 
}); 
+0

? Es lo que me gustaría evitar No usaré windows.location.href ... :) – markzzz

+0

event.preventDefault(); stringToSend = "? Param = 1"; var newUri = $ (this) .attr ('href') + stringToSend; $ (this) .attr ('href', newUri); – DenisPostu

+0

Disculpe por el formato del código, simplemente lo moveré al comentario original – DenisPostu

Cuestiones relacionadas