View Full Version : help trying to make a variable out of part of a string

09-24-2008, 04:20 PM
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.

<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);
document.write ('<a href="http://domain.co.uk/1?func=direct' + docnr + '">'); </script>

This is the url


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.


Philip M
09-24-2008, 04:42 PM
Try this:-

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