2012-07-17 15 views

risposta

37

Qualcosa di simile?

var querystring = 'myquerystringtoadd'; 

$('a').each(function() { 
    var href = $(this).attr('href'); 

    if (href) { 
     href += (href.match(/\?/) ? '&' : '?') + querystring; 
     $(this).attr('href', href); 
    } 
}); 

Working example.

+0

Meglio di @woz risposta, a causa del controllo per querystring esistente e sia aggiungendo con & o anteponendo? – jaygooby

+2

@jaygooby. Sì. Un ulteriore controllo dovrebbe essere fatto anche per gli ancoraggi url ("#"). –

+0

@flem -1: questo non funziona. Penso che dovrebbe essere 'href.match (/ \? /)'. – montrealist

1

Questa soluzione con javascript origini:

var querystring = 'yourQueryStringHere=;-)'; 

document.addEventListener('click', function (e) { 

    var x = e.originalTarget; 
    if (x.nodeName === 'A') { 

     var href = x.getAttribute('href'); 

     if(href) { 
      href += (/\?/.test(href) ? '&' : '?') + querystring; 
      x.setAttribute('href', href); 
     } 
    } 

}, false); 
Problemi correlati