url, :
Javascript
<script type="text/javascript">
function getAnchorValue(anchorId, key) {
var href = document.getElementById(anchorId).getAttribute('href');
var pageQuerySearch = new PageQuery(href.split('?')[1]);
return unescape(unescape(pageQuerySearch.getValue(key)));
}
function PageQuery(query) {
if (query.length > 1) {this.q = query; } else { this.q = null; } this.keyValuePairs = new Array();
if (this.q) { for (var i = 0; i < this.q.split("&").length; i++) { this.keyValuePairs[i] = this.q.split("&")[i]; } };
this.getValue = function (s) {
for (var j = 0; j < this.keyValuePairs.length; j++) {
if (this.keyValuePairs[j].split("=")[0] == s) { return this.keyValuePairs[j].split("=")[1]; }
} return false;
};
}
</script>
:
alert (getAnchorValue ('Anchor', 'myTag'));
JQuery
<script type="text/javascript">
; (function ($) {
$.extend({
getAnchorValue: function (name, url) {
function getQueryStringParams() {
var parameters = {}, e, a = /\+/g, r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = url ? url.split('?')[1] : window.location.search.substring(1);
while (e = r.exec(q)) { parameters[d(e[1])] = d(e[2]) }; return parameters;
}
if (!this.params) this.params = getQueryStringParams();
return this.params[name];
}
});
})(jQuery);
</script>
Using:
alert ($. getAnchorValue ('myTag', $ ('# Anchor'). attr ('href')));
EDIT : I edited my answer and also added jquery code to get the querystring parameter
source
share