Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Programmer needs you help! 1

Status
Not open for further replies.
Mar 25, 2003
34
0
0
GB
Hi all,

Can someone give me a clue what this function is doing?

I'm not, as you may guess, familiar with Javascript, but doesn't stop the boss asking me to look into it!

The function I believe, should be counting the number of jpg's in a folder 'charts' that start with a specific string ...

function getNberCharts(var4) {
var RE = new RegExp('(\\?|&)' + var4 + '=([^\\s]+)');
if
(location.search) return unescape(RE.exec(location.search)[2]);
}

var TotalNberCharts= getNberCharts('NberCharts');
 
The web page being rendered by the browser is represented according to the Document Object Model (DOM). One member of the window or frame is the location object. This object includes all information about the URL of the current document.

location.search is the querystring in the URL, beginning with the question mark. For example, I am looking at a document with the following URL


The value of location.search is

?qid=1070248&page=1


RegExp() is a Javascript object that is used to match patterns of text . The patterns are defined in a language called regular expressions. Using RegExp() is a bit complicated.

The variable RE in this code is a regular expression object that matches patterns such as

?something= anything except whitespace

I dont know why that \\ is in there. I think it means the character \, but I dont know why a URL would have \? in it.
In other words a pattern such as

?NberCharts=10
or such as

&NberCharts=gazillion

So there is nothing about jpg in that regular expression.

The function getNberCharts could actually be used to find any value of any name in the querystring. Just pass the name as the argument and the function would return the value.

unescape() simply decodes stuff like %20 which we sometimes see in querystrings. It returns the character represented by the URL encoding scheme.

The exec method of a RegExp instance such as RE returns an array of data.
The [2] element in the array is the substring of characters which match the pattern in the second set of parentheses. In other words whatever characters match

[^\\s]+

In my examples this would be 10 or gazillion. (Except as I said I dont know what those \\ are doing in there).

Simple, no?
 
Simple! I do love VB ...

Thanks for the reply ... it's starting to make sense.

Cheers
James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top