43

I am new to JQuery.

If I have the following tag. What is the best JQuery method to extract the value for "page" from the href.

<a href="Search/Advanced?page=2">2</a>

Malcolm

Malcolm
  • 12,524
  • 28
  • 89
  • 125

6 Answers6

71

The first thing that comes to my mind is a one-liner regex:

var pageNum = $("#specificLink").attr("href").match(/page=([0-9]+)/)[1];
Matchu
  • 83,922
  • 18
  • 153
  • 160
  • Note that this won't be perfect and will return false positives on things like `thepage=123`. Unless we can be sure that the URL always follows this exact structure, a full query parse might be more appropriate. – Matchu Jan 16 '12 at 22:34
  • 1
    We could also consider checking for `[?&]`at the start, as well. – Matchu Mar 15 '12 at 22:31
  • Just curious, what is the significance of the `[1]` at the end? – escist Jun 27 '12 at 08:33
  • 4
    @escist: match returns an array, the [1] element will be the part within the () group i.e. the digits. [0] will be the whole thing `page=2`. – Kinjal Dixit Jun 29 '12 at 05:59
  • This is such a nice simple method. Very good when you dont need a full parse. – The Thirsty Ape Jun 07 '13 at 19:19
17

I see two options here

var link = $('a').attr('href');
var equalPosition = link.indexOf('='); //Get the position of '='
var number = link.substring(equalPosition + 1); //Split the string and get the number.

I dont know if you're gonna use it for paging and have the text in the <a>-tag as you have it, but if you should you can also do

var number = $('a').text();
Kenny Eliasson
  • 2,047
  • 15
  • 23
  • Your first solution will not work if there are other arguments in the link: Search/Advanced?search=mysearch&page=2 Another issue is that it only works with single character pages. It will not work with page 12 for example – Nadia Alramli May 16 '09 at 11:58
  • Yes, I know about the other arguments, maybe should included that in the answer. But why wouldn't it work with multiple characters? – Kenny Eliasson May 16 '09 at 12:23
  • The answer does work for multiple characters. In fact I just tested this in Firebug. The single argument to substring is the starting location of the substring, if a second argument is not specified it will just read to the end of the string. Nadia does bring up a good point about multiple arguments, though. – Justin Ethier May 17 '09 at 13:51
6

First of all you need to extract the path with something like this:

$("a#myLink").attr("href");

Then take a look at this plugin: http://plugins.jquery.com/project/query-object

It will help you handle all kinds of querystring things you want to do.

/Peter F

Peter Örneholm
  • 2,838
  • 20
  • 24
4

Here's a method that works by transforming the querystring into JSON...

var link = $('a').attr('href');

if (link.indexOf("?") != -1) {
    var query = link.split("?")[1];

    eval("query = {" + query.replace(/&/ig, "\",").replace(/=/ig, ":\"") + "\"};");

    if (query.page)
        alert(unescape(query.page));
    else
        alert('No page parameter');

} else {
    alert('No querystring');
}

I'd go with a library like the others suggest though... =)

great_llama
  • 11,481
  • 4
  • 34
  • 29
0

Use this jQuery extension by James Padoley

neshpro9
  • 433
  • 3
  • 17
Iain Holder
  • 14,172
  • 10
  • 66
  • 86
-1
if ($('a').on('Clicked').text().search('1') == -1)
{
    //Page == 1
}
else
{
    //Page != 1
}
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156