Skip to content

Instantly share code, notes, and snippets.

@tobsn
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobsn/ae4ae937edbb95e5bc57 to your computer and use it in GitHub Desktop.
Save tobsn/ae4ae937edbb95e5bc57 to your computer and use it in GitHub Desktop.
replace query variables within static html with javascript and jQuery as selector
// http://jsfiddle.net/96q4m/17/ - old
// simulating GET variables
if(window.location.search.indexOf('test') < 0) window.location.search = window.location.search+'?test=123&img=https%3A%2F%2Fdrupal.org%2Ffiles%2Ftest-all-the-things.jpg';
// remove everything above this line - stop removing after
// closure compiled:
$(function(){function a(a){return decodeURIComponent(a.replace(b," "))}for(var b=/\+/g,c=/([^&=]+)=?([^&]*)/g,d=window.location.search.substring(1);y=c.exec(d);)$("#tpl-"+a(y[1])).length&&("IMG"!==$("#tpl-"+a(y[1]))[0].tagName?$("#tpl-"+a(y[1])).html(a(y[2])):$("#tpl-"+a(y[1])).attr("src",a(y[2])))});
/* clean code
$(function() {
var q = /\+/g,
u = /([^&=]+)=?([^&]*)/g,
r = window.location.search.substring( 1 ),
e = function( s ) {
return decodeURIComponent( s.replace( q, ' ' ) );
};
while( y = u.exec( r ) ) {
if( $( '#tpl-' + e( y[1] ) ).length ) {
if( $('#tpl-' + e( y[1] ) )[0].tagName !== 'IMG' ) {
$( '#tpl-' + e( y[1] ) ).html( e( y[2] ) )
} else {
$( '#tpl-' + e( y[1] ) ).attr( 'src', e( y[2] ) )
}
}
}
});
*/
<div id="tpl-test"></div>
<img id="tpl-img" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment