Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da" lang="da">
<head>
<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="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="jquery.ba-url.js" 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 http://benalman.com/projects/jquery-url-utils-plugin/
* usage:
* From the HEAD element, refer to the following two script files:
* http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
* jquery.ba-url.js
*
*/
</script>
</head>
<body>
<h1>Find all a elements, and append to its querystring</h1>
<h2>These should be modified</h2>
<ul>
<li><a href="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" >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>
</ul>
<h2>These should not be modified (starting with javascript:)</h2>
<ul>
<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>
</ul>
<h2>Usage:</h2>
<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 );
$('a[href]').each(function(){
url = this.href;
if (url.match(/^javascript/i))
{
//do not append
}
else
{
$(this).queryString( params);
}
});
$('a').each(function(){
if (this.href.match( params) === null)
{
var cssObj = {'background-color': '#fcc'};
$(this).css(cssObj).append(' <small>(&lt;==has no params appended)</small>');
}
});
</script>
</body>
</html>
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.