Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2008, 03:20 PM   PM User | #1
yabai
New to the CF scene

 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
yabai is an unknown quantity at this point
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.
yabai is offline   Reply With Quote
Old 09-24-2008, 03:42 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:10 AM.


Advertisement
Log in to turn off these ads.