0

I have pagination links set-up like this:

http://localhost/?page=2
http://localhost/?page=3

They are wrapped in Anchor links as the HREF attribute. I want to know how can I check first if the HREF attribute for a given ANCHOR contains the query string "page" case sensitive, and if it exists return its number the value after page=

Please give me a straightforward example on this, much appreciated. :)

3 Answers 3

1

You could try something like this:

function getPageNumber( $obj ){
  if( $obj.filter('a[href*=page\=]').length )
    return $obj.attr('href').split('page=')[1];  
  else
    return false;
}

Then with:

<a id="foo" href="http://localhost/?page=3">Foo</a>
<a id="bar" href="http://localhost/?page=4">Bar</a>
<a id="baz" href="http://localhost/?Page=5">Baz</a> <!--- capital P --->

You'd get:

var result = getPageNumber( $('a#foo') ); // returns 3
var result = getPageNumber( $('a#bar') ); // returns 4
var result = getPageNumber( $('a#baz') ); // returns false (case sensitive)

Of course it would be easy to write the function to take a plain DOM object, or an ID, or whatever else you have in mind.

Sign up to request clarification or add additional context in comments.

Comments

0
$("a").each(function() {
    var href = this.attr("href");
    if(href.match(/page/)) {
        pageNumber = parseInt(href.split('=')[1]);
        doSomething(this, pageNumber); //this is the jQuery element
    }
});

Comments

0

If you have a point to the anchor tab and the format given by you is consistent, then it can be done with something as given below

var a = document.getElementById("#myanchor");
if(/[\?&]page=/.test(a.href){
    alert("parameter page present");
}

Here we are looking for the parameter page with the help of javascript regular expression.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.