Skip to content

Commit 5c005a3

Browse files
committed
Query passer script - Vanilla and Jquery versions
1 parent a30740a commit 5c005a3

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
# Query-passer
2-
Passing query parameters from the URL to HREF links
2+
3+
Query-passer is a simple script that passes all query strings from the current URL to all existing `<a>` tags in a page.
4+
5+
The script helps you to keep the query strings across all pages in your website.
6+
7+
Hashtag anchors (`<a href="#info">`) will be placed after the query strings (`<a href="?cid=1#info">`).

queryPasser.jquery.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
$(document).ready(function() {
2+
$(function() {
3+
var href;
4+
$('a').each(function() {
5+
href = $(this).attr('href');
6+
if (href !== "undefined" && href != null && href && href.length > 1) {
7+
if (href !== 'undefined' || href.substr(0) != '' || href !== null) {
8+
if (href.substr(0, 1) == '#') {
9+
$(this).attr('href', location.search + href);
10+
} else if (href.indexOf('#') > 0) {
11+
href = href.split('#');
12+
$(this).attr('href', href[0] + location.search + '#' + href[1]);
13+
} else {
14+
href += location.search;
15+
$(this).attr('href', href);
16+
}
17+
}
18+
}
19+
});
20+
});
21+
});

queryPasser.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(function() {
2+
var href;
3+
document.querySelectorAll('a').forEach(function(ele){
4+
href = ele.getAttribute('href');
5+
if (href !== "undefined" && href != null && href && href.length > 1) {
6+
if (href !== 'undefined' || href.substr(0) != '' || href !== null) {
7+
if (href.substr(0, 1) == '#') {
8+
ele.setAttribute('href', location.search + href);
9+
} else if (href.indexOf('#') > 0) {
10+
href = href.split('#');
11+
href[0] + location.search + '#' + href[1]
12+
ele.setAttribute('href', href[0] + location.search + '#' + href[1]);
13+
} else {
14+
href += location.search;
15+
ele.setAttribute('href', href);
16+
}
17+
}
18+
}
19+
});
20+
})();

0 commit comments

Comments
 (0)