473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Extracting parameters

I found this little script for extracting parameters from a url but wondered
what the shortest and most efficient way to do it would be, like the
following or via regexp?

function getParameter(paramName) {
var currentUrl = window.location.search
var strBegin = currentUrl.indexOf(paramName) + (paramName.length+1)
var strEnd = currentUrl.indexOf("&",strBegin)

if (strEnd==-1)
strEnd = currentUrl.length

return currentUrl.substring(strBegin,strEnd)
}
Any shorter way?
Thanks,

Steffan

Jun 2 '07 #1
1 1579
Phat G5 (G3) wrote on 02 jun 2007 in comp.lang.javascript:
I found this little script for extracting parameters from a url but
wondered what the shortest and most efficient way to do it would be,
like the following or via regexp?

function getParameter(paramName) {
var currentUrl = window.location.search
var strBegin = currentUrl.indexOf(paramName) +
(paramName.length+1) var strEnd = currentUrl.indexOf("&",strBegin)

if (strEnd==-1)
strEnd = currentUrl.length

return currentUrl.substring(strBegin,strEnd)
}
Any shorter way?
<script type='text/javascript'>

function getParameter(p) {
var re = new RegExp('&'+p+'=([^&]*)','i');
var c = '?apple=steve&pc=bill&a='; //window.location.search
return (c=c.replace(/^\?/,'&').match(re)) ?c=c[1] :c='NULL';
};

// Testing:
alert(getParameter('pc'));
alert(getParameter('qqq'));
alert(getParameter('aPPLE'));
alert(getParameter('A'));

</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 2 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nazgul | last post by:
Hi! I want to implement a small tool in Python for distributing "patches" and I need Your advice. This application should be able to package all files chosen by a user into a self-extracting.exe...
5
by: Astra | last post by:
Hi All Is there an ASP way of extracting the height and width of a swf file so that I can specify these dims when adding the whole OBJECT code to the web page? Thanks Robbie
3
by: wenz | last post by:
Hi I have an exe which is actually a self-extracting zip file that i would like to run to extract all the files. I have the code showing how to use the j# classes and library to extract zip files...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
2
by: Robert McEuen | last post by:
Sorry if this double-posts...Google doesn't do a very good job of communicating whether something has posted or not. Using Access 97, WindowsXP Is there a way to pass command line parameters...
1
by: annaannie | last post by:
hello sir, My aim is to extract 'id' and 'ac' from given XML files,and store the results in two different files.the code i wrote can extract 'ids',and give the output in a file.But i cant extract...
6
by: sunil | last post by:
I have a button named Button1, and I wrote an event handler for the OnClick event. protected void Button1_Click(object sender, System.EventArgs e) { this.Response.Redirect("Default.aspx?q=" +...
4
by: davidiwharper | last post by:
Hi everyone, I am rewriting a basic "email data to administrator" script to include an error check, which will ensure that all fields are filled with at least some data. However, there is a...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.