Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  156] [ 5]  / answers: 1 / hits: 37044  / 10 Years ago, mon, june 30, 2014, 12:00:00

On a webpage that has a list of categories, and each category title is linked in this format: http://localhost/admin/category/unpublish/2



I wrote the following js code, trying to capture the url and the segments 'unpublish' (action) and '2' (id), and need to send the request to http://localhost/admin/category



$('#statusChanges a').click(function(evt) { // use the click event of hyperlinks
evt.preventDefault();
var url = $(location).attr('href');
// var action = url.segment(3); /*JS console complains that url.segment() method undefined! */
// var id = url.segment(4);
$.ajax({
type: GET,
url: $(location).attr('href'),
dat: '',
/* do I need to fill the data with json data: {action: unpublish, id: 2 } ? but I don't know how to get the segments */
success: function(data) {
$('.statusSuccess').text('success!');
},
error: function(data) {
$('.statusSuccess').text('error!');
}
});
}); // end of status change

More From » jquery

 Answers
97

Try this



var url = $(location).attr('href').split(/).splice(0, 5).join(/);


Update Answer:



User this object to get current anchor link see below



$(this).attr('href')

[#70380] Friday, June 27, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blair

Total Points: 384
Total Questions: 108
Total Answers: 86

Location: Northern Ireland
Member since Tue, May 5, 2020
4 Years ago
;