Reputation: 5450
hi i need to replace all the href's in my page by a onclick event which has few of the parameters from the query string
here is a sample anchor tag
<a href="google.com?businessunit=Products&dataSegement=BOTH_PERIOD&endDate=12%2F06%2F2011&d-49489-s=8&d-49489-p=1&d-49489-o=2&catid=3">Demo Anchor</a>
another sample
<a href="google.com?businessunit=Products&dataSegement=BOTH_PERIOD&pubgrpid=6&endDate=12%2F06%2F2011&d-49489-s=8&d-49489-p=1&d-49489-o=2&marketid=1analysisType=conversion&catid=3">Another sample</a>
i need to extract the values of d-49489-s, d-49489-p, d- 49489-o and change anchor tag to something like
<a href="#" onclick="callMethod(d-49489-s,d-49489-p, d-49489-o )">
Upvotes: 0
Views: 3367
Reputation: 14501
$('a').each(function(e){
var queryString = this.href;
queryString = queryString.substring(queryString.indexOf('?'), queryString.length);
var params = new Array();
while (queryString)
{
var start = queryString.indexOf('&d-')+1;
var end = start+9;
if (start) { //if param found, save and truncate query string
params.push(queryString.substring(start, end));
queryString = queryString.substring(end, queryString.length);
}
else
queryString = '';
}
var functionCall = 'callMethod('+params[0]+', '+params[1]+', '+params[2]+');';
$(this).attr('onclick', functionCall);
});
Upvotes: 1
Reputation: 2773
You don't have to go through all the links on the page. Use unobtrusive JavaScript to intercept clicks to the links and then return false in the event handler to prevent the browser from navigating to the URL specified in the anchor's href attribute.
Also, if you are generating the page yourself, you don't have to parse the href, you can use html5 data-
attributes to store the function arguments for your JavaScript calls:
HTML:
<a href="asdf.html?a=b&c=d" data-ajax-params="{"a": "b", "c": "d"}">Test</a>
<a href="asdf.html?a=b&c=d" data-ajax-params="["b", "d"]">Test</a>
JavaScript:
$(function() {
$('a').click(function() {
//first link will return object with key=value pairs, second link will return an array with just the values
console.log($(this).data('ajax-params'));
//do something with the parameters
//make sure to return false to prevent the browser from navigating to href URL
return false;
});
});
Of course, instead of using the data-
attribute, you can always do some parsing of the href as suggested in other answers. But I would not replace all the hrefs when the page is loaded. It's simply an overhead to replace all href attributes, while the user might click only on one of them, or maybe leave the page without clicking anything!
Upvotes: 0
Reputation: 253318
One approach:
$('a').each(
function(){
var h = this.href;
var r = h.split('&');
var reqs = [];
for (i=0;i<r.length;i++){
if (r[i].indexOf('d-') == 0){
reqs.push(r[i].substring(0,r[i].indexOf('=')));
}
}
$(this).attr('onclick','callMethod(' + reqs.join(',') + ')');
});
References:
Upvotes: 3
Reputation: 678
Pretty sure you can do this with the BBQ plugin:
http://benalman.com/projects/jquery-bbq-plugin/
It'll go through all hrefs on a page and edit them. It let's you tokenize the query parameters really easily. There are examples on the site.
Upvotes: 0