Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help trying to make a variable out of part of a string

    hi. i am new to javascript and am asking if someone can point me to a good resource or let me know what terms I should be searching for.

    I have a site with a page that uses javascript to take the url, checks it and then writes out a new one.
    Code:
    <script type="text/javascript"> 
    var str = document.URL; 
    //start from doc_library rather than doc_number 
    var startdocnr = str.indexOf("&doc_library="); 
    //alert (startdocnr); 
    var enddocnr = str.indexOf("&year="); 
    //alert (enddocnr); 
    var docnr = str.substring(startdocnr,enddocnr); 
    //alert(docnr); 
    document.write ('<a href="http://domain.co.uk/1?func=direct' + docnr + '">'); </script>
    This is the url
    Code:
    http://domain.co.uk/1&func=item-global&doc_library=abc01&doc_number=003764840&year=&volume=&sub_library=
    I would like to amend the javascript so that it will work if i come from a different url which doesn't have &year=&volume=&sub_library= in the url

    Therefore i think I need to work out some code that makes the variable 'docnr' by finding the string 'doc_number=' and then the next 9 digits. What is this type of task called in javascript. Any pointers or examples would be much appreciated.

    Thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Try this:-

    Code:
    var str = "http://domain.co.uk/1&func=item-global&doc_library=abc01&doc_number=003764840"
    var docnr =  str.match(/doc_number=\d{9}/);
    alert (docnr);

    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 09-24-2008 at 03:51 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •