Web Analytics Made Easy -
StatCounter extracting part of string - CodingForum

Announcement

Collapse
No announcement yet.

extracting part of string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • extracting part of string

    Hi

    Can anyone help please ... I am only a novice at javascript.

    I have a field called "index_name[counter].url" which is very long it has 30-40 characters ........ although it is always in the same format ........

    http://www.mysite.com/action.pl?n33253396&f=409790

    What code would I have to use to extract "33253396" from the rest of the code and call it something like "num" ....

    Any help would be appreciated .... but please be kind and assume I know nothing! The more precise you can be the better.

    Many thanks to anyone who can help.

    snow

  • #2
    url = location.search;
    num = url.substring(2,url.indexOf("&"));

    Comment


    • #3
      If you are dealing with different URLS, using regular expressions would be a better way to go. Just food for thought.
      Create accessible online surveys -::- Koobten.com - compare netbook prices and reviews -::- Affordable, reliable hosting for less than $20 per year
      Zend Certified Engineer

      Comment


      • #4
        Sorry doesn't work !

        Sorry Guys ....

        This doesn't work .... or at least I don't have the knowledge to make it work ..........

        I have a field which when I print it into a page is called:

        +"index_name[counter].url"+

        Now instead of printing that I want to print a field called:

        +"num"+



        So to do that, before the print routine I type:

        num = index_name[counter].url;



        But that prints all of it, namely:

        http://www.mysite.com/action.pl?n33253396&f=409790



        All I want is ...........33253396

        So what do I put after or instead of (please state exactly which) the line:

        num = index_name[counter].url;

        to make it happen .............



        Sorry if I am being stupid ..... but I am new to this and struggling a bit ..... any help would be appreciated!

        Thanks

        snow

        Comment


        • #5
          num = index_name[counter].url;
          num = num.slice(num.indexOf('?')+1,num.indexOf('&'));

          that'll give you a string representation of the number. if you want it to actually be a number, so that you can add or subtract from it, then you have to add one more line:

          num = Number(num);

          hope that does what you want.
          bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

          i am a loser geek, crazy with an evil streak,
          yes i do believe there is a violent thing inside of me.

          Comment


          • #6
            Thanks

            Thanks ... that's excellent. It works well

            snow


            Comment

            Working...
            X