2010-04-19 9 views
16

Come aggiungo diciamo qualcosa come ajax=1 a tutti i link sulla mia pagina con jquery. Dovrò anche verificare se l'url ha parametri esistenti. per esempio http://example.com/index.php?pl=132 dovrà diventare http://example.com/index.php?pl=132&ajax=1aggiungi parametro ai collegamenti sulla pagina utilizzando jquery

Inoltre, se il link non ha alcun parametro, ad esempio http://example.com/index.php, diventerà http://example.com/index.php?ajax=1 voglio caricare lo script jQuery sul documento pronto in modo che tutti i collegamenti vengono modificate al caricamento della pagina.

risposta

2

Se siete interessati a plugin, c'è il jQuery Query String Object. Ciò consentirà un semplice controllo dei parametri nella querystring e, se necessario, la possibilità di aggiungerne altri, rimuoverne alcuni o modificarne altri.

+3

Questo non è quello che sta cercando. – SLaks

+0

@SLaks Non sono d'accordo.Ciò realizza ciò che hai offerto, ma racchiuso in un plug-in e meno dettagliato. È possibile verificare se esiste un parametro, in caso contrario, aggiungerlo, ecc. – Sampson

+0

È possibile modificare i collegamenti ipertestuali esistenti? – SLaks

49

Si potrebbe fare qualcosa di simile:

$(function() { 
    $("a").attr('href', function(i, h) { 
    return h + (h.indexOf('?') != -1 ? "&ajax=1" : "?ajax=1"); 
    }); 
}); 

Su questo document.ready guarda ogni <a>, guarda è href, se contiene ? già che aggiunge &ajax=1 se non lo fa, aggiunge ?ajax=1.

+0

Fatta eccezione per ' /weird/page.aspx? '. Inoltre, questo non funzionerà per gli ancoraggi con nome. – SLaks

+0

@SLaks - Il problema è il rendering iniziale del collegamento in quel caso, non jQuery ... –

+0

AFAIK, questo è un URL legale (ma molto improbabile). – SLaks

5

Ti piace questa:

$(function() { 
    $('a[href]').attr('href', function(index, href) { 
     var param = "key=value"; 

     if (href.charAt(href.length - 1) === '?') //Very unlikely 
      return href + param; 
     else if (href.indexOf('?') > 0) 
      return href + '&' + param; 
     else 
      return href + '?' + param; 
    }); 
}) 
+1

ha testato lo script. le ancore nominate falliscono. link come "mysite.com/wiki/something" non vengono aggiunti .... – Priyo

+0

Per qualche motivo questo aggiunge la mia coppia chiave-valore due volte? Come:? Key = value & key = value – stoerebink

2

Prendendo ciò che è sopra, questo è il modo migliore per concatenare i parametri ai collegamenti.

Evitare anche il collegamento con href come href = "Javascript: YourFunction();"

$(function(){          
    var myRandom = getRandom();  
    $('a[href]').each(function(i){ 
     var currHref = $(this).attr("href"); 
     var isAfunction = currHref.substring(0,10);    
     if(isAfunction == "javascript"){     
      $(this).attr("href",currHref); 
     }else{ 
      if (currHref.charAt(currHref.length - 1) === '?')       
       $(this).attr("href",currHref); 
      else if (currHref.indexOf('?') > 0)     
       $(this).attr("href",currHref+"&rand="+getRandom()); 
      else     
       $(this).attr("href",currHref+"?rand="+getRandom()); 
     } 
    });    
}); 
function getRandom(){ 
    var randomnumber = Math.floor(Math.random()*10000); 
    return randomnumber; 
} 
4

Ecco una soluzione che ho messo insieme per nativo JavaScript, supporta stringhe di query e le ancore esistenti:

function addToQueryString(url, key, value) { 
    var query = url.indexOf('?'); 
    var anchor = url.indexOf('#'); 
    if (query == url.length - 1) { 
     // Strip any ? on the end of the URL 
     url = url.substring(0, query); 
     query = -1; 
    } 
    return (anchor > 0 ? url.substring(0, anchor) : url) 
     + (query > 0 ? "&" + key + "=" + value : "?" + key + "=" + value) 
     + (anchor > 0 ? url.substring(anchor) : ""); 
} 

ho postato i miei test esistenti su JSBin: http://jsbin.com/otapem/2/

Problemi correlati