tooltips bootstrap javascript jquery

javascript - bootstrap - añadir parámetro a los enlaces en la página usando jquery



tooltip html (6)

Aquí hay una solución que armé para Javascript nativo, que admite cadenas de consulta y anclajes existentes:

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) : ""); }

He publicado mis pruebas existentes en JSBin: http://jsbin.com/otapem/2/

Cómo agrego, digamos algo como ajax=1 a todos los enlaces en mi página con jquery. También necesitaré verificar si la URL tiene parámetros existentes. por ejemplo, http://example.com/index.php?pl=132 tendrá que convertirse en http://example.com/index.php?pl=132&ajax=1

Además, si el enlace no tiene ningún parámetro, por ejemplo, http://example.com/index.php , se convertirá en http://example.com/index.php?ajax=1 Quiero cargar el script jQuery en el documento listo para que todos los enlaces se cambien en la carga de la página.


Me gusta esto:

$(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; }); })


Podrías hacer algo como esto:

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

En document.ready esto se ve en cada <a> , mira su href, si contiene ? ya agrega &ajax=1 si no lo hace, agrega ?ajax=1 .


Si está interesado en complementos, está el objeto de cadena de consulta jQuery. Esto le permitirá una comprobación simple de los parámetros en la cadena de consulta y, si es necesario, la posibilidad de agregar más, eliminar algunos o editar otros.


Tomando lo que está arriba, esta es la mejor manera de concatenar parámetros a los enlaces.

También evita el enlace con href como 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; }


Un href debe estar lleno y corregido.

$(function() { $(''a[href]'').attr(''href'', function(index, href) { var url = new URL(href); url.searchParams.set(''ajax'',1); return url; }); })

JSFiddle