Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" xml:lang="da" lang="da">
<title>jquery find all a elements and append to URLs only</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="" type="text/javascript"></script>
<script src="" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
/* Merge an arbitrary query string into all page links.
* now will ignore javascript: links
* requires jQuery 1.3.2 and "jQuery URL utils" plugin
* usage:
* From the HEAD element, refer to the following two script files:
<h1>Find all a elements, and append to its querystring</h1>
<h2>These should be modified</h2>
<li><a href="" >link to jquery (external server)</a></li>
<li><a href="/url1">link to url1</a></li>
<li><a href="/url2.html">link to url2.html</a></li>
<li><a href="url3.cfm?querystring=already">link to url3.cfm?querystring=already</a></li>
<li><a href="url3.cfm?q=10&amp;hl=en">link to url3.cfm?q=10&amp;amp;hl=en</a></li>
<li><a href="url3.cfm?q=10&amp;hl=en">link to url3.cfm?q=10&amp;hl=en</a> (not escaped ampersand)</li>
<li><a href="">empty link</a></li>
<LI><A HREF="URL3.cfm?1=3">UPPERCASE LINK</A> (deprecated uppercase HTML usage)</LI>
<h2>These should not be modified (starting with javascript:)</h2>
<li><a href="Javascript:alert('hellow')" >simple alert call</a> </li>
<li><a href="javascript:toggleClass('collapse')">link to javascript method</a></li>
<li><a onclick="real_javascript_method()">A with onclick and not href=javascript!</a></li>
<p>view source for details.</p>
<p>Script added by Jesper Rønn-Jensen, 2009-09-20</p>
<pre id="logwindow"></pre>
<script type="text/javascript">
// Merge an arbitrary query string into all page links.
var params = 'x=1&y=2';
//$('a:not([href^="javascript"])').queryString( params );
url = this.href;
if (url.match(/^javascript/i))
//do not append
$(this).queryString( params);
if (this.href.match( params) === null)
var cssObj = {'background-color': '#fcc'};
$(this).css(cssObj).append(' <small>(&lt;==has no params appended)</small>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.