Substr Function in JQuery

documenthref.jpg
Please see the attached image.  I am trying to pull the document id from this href.  You can see it at latestfordocid=445.  Its the 445 that I need.

My jquery to extract the docid is as follows:

$(document).on("click", "a", function() {
    //this == the link that was clicked
    var href = $(this).attr("href");
   
    var myString = href.substr(href.indexOf("?") + 1);  ********

    var myString2 = myString.substr(15,3);

The ****** line is throwing an error in the Chrome browser.  

Is there something else I can use instead of substr that will work in the most popular browsers.

Thanks in advance.
sherbug1015Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I think you are perhaps mixing up what is going on server side with asp code and client side with js/jquery.

To really understand the problem, you should create a very simple test case and post the link so we can see the issue using the browser's console.  The error message you show is really of no help here.

This works in chrome
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script>
    $(function(){
      $(document).on("click", "a", function() {
    //this == the link that was clicked
    var href = $(this).attr("href");
    
    var myString = href.substr(href.indexOf("?") + 1);

    var myString2 = myString.substr(2,3); 
  
  $("#results").html("<hr>"+href+"<hr>"+myString+"<hr>"+myString2+"<hr>");
  
});
    });
  </script>
  <meta charset="utf-8">
  <title>http://www.experts-exchange.com/questions/28693824</title>
</head>
<body>
  <a href="mydomain.com?x=123">click me</a>
  <div id="results"></div>
</body>
</html>

Open in new window


The part I did have to switch is the line below in order to match up with what I was expecting the query string to be.
   var myString2 = myString.substr(2,3);
0
sherbug1015Author Commented:
hreferror.jpg
Here is the error that is being thrown.  

My question is is there a way to extract the docid without using the substr function?
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
please post a link to your sample page or post sample code including html that can easily be replicated on jsbin or jsfiddle.  can't do anything with images
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

sherbug1015Author Commented:
I don't think we are on the same page.  The error that is being thrown is not the issue.  The issue is I have been asked not to use the substring function.  

My question is how to extract the three numbers after latestfordocid=    
without using the substring function.    For example I can remove everything up to the question mark using regex var mystring = href.replace(/.*\?/,'');.  

What I need help with is how to extract the 3 numbers after latestfordocid without using substring.  



documenthref.jpg
0
sherbug1015Author Commented:
I was able to solve this myself

var href = $(this).attr("href");
    var mystring = href.replace(/.*\?/,'');  //remove everything before the ?
    var mystring1 = mystring.replace(/\&.*/,''); //remove the hash key
    var mystring2 = mystring1.replace(/[^\d\.]/g, ''); // remove non-numerics

mystring2 will now contain just the document id that I need.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:
My suggestion would have been to use a function like "gup" (short for 'get url parameter'):
http://www.netlobo.com/url_query_string_javascript.html

and modify it to accept an optional url as the second argument.  That way, IF you don't provide a second argument, it will take the url to be the browser's current url in the address  bar.  The other added benefit is that it does not depend on the position of the parameter within the url.  If "latestfordocid" were to be "moved" to the middle or the end of the url (for whatever reason),
the function below will still work.
function gup( name,context )
{
 if(!context)
  context=window.location.href;
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "(^|[\\?]|&(amp;)?)"+name+"=([^&#]*)";
  var regex = new RegExp( regexS,"" );
  var results = regex.exec( context );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[3]);
}

//then call it as follows:
var docid = gup('latestfordocid',href);
var hash = gup('hash',href);
var chset = gup('chset',href);
alert( docid );
alert( hash );
alert( chset );

Open in new window

0
sherbug1015Author Commented:
My solution fixes my issue.  

var href = $(this).attr("href");
    var mystring = href.replace(/.*\?/,'');  //remove everything before the ?
    var mystring1 = mystring.replace(/\&.*/,''); //remove the hash key
    var mystring2 = mystring1.replace(/[^\d\.]/g, ''); // remove non-numerics
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.