473,657 Members | 2,540 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

url parsing

I use the following to parse the url

var srch = window.location .search.substri ng(1);
// then split srch at the ampersand:
var parts = srch.split("&") ;
// write the parameters into the variables
for(var i in parts) {
var temp = parts[i].split("=");
if (temp[0] == "xx") { xx = 1 * temp[1]; }
if (temp[0] == "yy") { yy = 1 * temp[1]; }
if (temp[0] == "ff") { ff = 1 * temp[1]; }
if (temp[0] == "level") { level = 1 * temp[1]; }
}

However, when I parse the url, it consistently chokes when presented
with an ampersand character. Do I need to escape this character somehow?
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #1
28 2827
Fabian hu kiteb:
I use the following to parse the url

var srch = window.location .search.substri ng(1);
// then split srch at the ampersand:
var parts = srch.split("&") ;


This line is definately the big isue. When I use "a" instead of "&", it
parses multiple substrings correctly.

var parts = srch.split("a") ;

Unfortunately, this isn't the standard approach to including data in an
url, so while it works, it is sub-optimal.

--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #2
"Fabian" <la****@hotmail .com> writes:
I use the following to parse the url

var srch = window.location .search.substri ng(1);
// then split srch at the ampersand:
var parts = srch.split("&") ; .... However, when I parse the url, it consistently chokes when presented
with an ampersand character. Do I need to escape this character somehow?


That depends.

If the code is embedded in an HTML page, then the ampersand should be
escaped (as an HTML entity, "&amp;", not a Javascript string escape
"\&"). If it is in an external javascript file, then it should not.

So, probably. Have you tried escaping it?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
Lasse Reichstein Nielsen hu kiteb:
However, when I parse the url, it consistently chokes when presented
with an ampersand character. Do I need to escape this character
somehow?


That depends.

If the code is embedded in an HTML page, then the ampersand should be
escaped (as an HTML entity, "&amp;", not a Javascript string escape
"\&"). If it is in an external javascript file, then it should not.

So, probably. Have you tried escaping it?


It is in an external javascript file. I have tried escaping it, and none
of the escape sequences I know seem to make much different. I've tried
%26, , &amp;, and \u0026. I'm not aware of other methods for
escaping the character.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk
Jul 20 '05 #4
Fabian wrote:
var srch = window.location .search.substri ng(1);
// then split srch at the ampersand:
var parts = srch.split("&") ;
// write the parameters into the variables
for(var i in parts) {
Not so. If successful, String.split(.. .) returns an Array object.
Iterate only over the array's elements, not over all the object's
enumerable properties:

for (var i = 0; i < parts.length; i++)
{
// ...
}
var temp = parts[i].split("=");
if (temp[0] == "xx") { xx = 1 * temp[1]; }
xx = +temp[1];

is faster (should be available from Netscape 4.06 on.)
I do hope `xx' aso. are not global variables used in a
local execution context.
if (temp[0] == "yy") { yy = 1 * temp[1]; }
if (temp[0] == "ff") { ff = 1 * temp[1]; }
if (temp[0] == "level") { level = 1 * temp[1]; }
Make use of either switch...case

switch (temp[0])
{
case "yy": yy = +temp[1]; break;
case "ff": ff = +temp[1]; break;
case "level": level = +temp[1]; break;
}

or a container object (here: window):

window[temp[0]] = temp[1];
}

However, when I parse the url, it consistently chokes when presented
with an ampersand character. Do I need to escape this character somehow?


Yes, RFC 2396 states that `&' is a special character and must be
escaped in URIs if not used with that special meaning. Since its
decimal ASCII code is 38, it can be escaped in URIs with the
hexadecimal representation of `%26'.

You may find http://pointedears.de.vu/scripts/search.htm useful.
(Please pay attention to the license agreement.)
Merry Christmas! (if you celebrate it)

PointedEars
Jul 20 '05 #5
Fabian wrote:
It is in an external javascript file. I have tried escaping it, and none
of the escape sequences I know seem to make much different. I've tried
%26, , &amp;, and \u0026. I'm not aware of other methods for
escaping the character.


`%26' and `\u0026' represent characters different from `'. While
the former are hexadecimal representations of the code of the character,
the latter is a decimal one. Only `&#x26;' (case-insensitive) and
`&#x0026;' would represent the same as the former.

http://www.w3.org/TR/html4/charset.html#h-5.3.1
PointedEars
Jul 20 '05 #6
Thomas 'PointedEars' Lahn hu kiteb:
if (temp[0] == "xx") { xx = 1 * temp[1]; }
xx = +temp[1];


I made this change about 5 minutes after posting when I saw a similar
response to making javascript recognise strings as numbers.
Make use of either switch...case

switch (temp[0])
{
case "yy": yy = +temp[1]; break;
case "ff": ff = +temp[1]; break;
case "level": level = +temp[1]; break;
}


Now using this code. Thanks.
However, when I parse the url, it consistently chokes when presented
with an ampersand character. Do I need to escape this character
somehow?


Yes, RFC 2396 states that `&' is a special character and must be
escaped in URIs if not used with that special meaning. Since its
decimal ASCII code is 38, it can be escaped in URIs with the
hexadecimal representation of `%26'.


I tried using srch.split("%26 ") but that would not split on an actual
question mark, only the literal string "%26". It finally turns out that
what I need is to split on "\?".

var srch = window.location .search.substri ng(1);
var parts = srch.split("\?" );
for (var i = 0; i < parts.length; i++) {
var temp = parts[i].split("=");
switch (temp[0]) {
case "xx": xx = +temp[1]; break;
case "yy": yy = +temp[1]; break;
case "ff": ff = +temp[1]; break;
case "ll": level = +temp[1]; break;
}
}

Does this look good?
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk
Jul 20 '05 #7
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Not so. If successful, String.split(.. .) returns an Array object.
Iterate only over the array's elements, not over all the object's
enumerable properties:
The only enumerable properties of an Array instance are the elements
of the array by default. All methods of Array.prototype and the length
property are non-enumerable.
for (var i = 0; i < parts.length; i++)


So unless you have extended Array.prototype or Object.prototyp e, this is
equivalent to
for (var i in parts)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@we b.de> writes:
Not so. If successful, String.split(.. .) returns an Array object.
Iterate only over the array's elements, not over all the object's
enumerable properties:


The only enumerable properties of an Array instance are the elements
of the array by default. All methods of Array.prototype and the length
property are non-enumerable.


Maybe, but I prefer it this way since it is independent
of flawed implementations and extensions to the prototype.
for (var i = 0; i < parts.length; i++)


So unless you have extended Array.prototype or Object.prototyp e, this is
equivalent to
for (var i in parts)


It is not since the order of the retrieved properties is
then undefined.
PointedEars
Jul 20 '05 #9
Fabian wrote:
Thomas 'PointedEars' Lahn hu kiteb:
However, when I parse the url, it consistently chokes when presented
with an ampersand character. Do I need to escape this character
somehow?
Yes, RFC 2396 states that `&' is a special character and must be
escaped in URIs if not used with that special meaning. Since its
decimal ASCII code is 38, it can be escaped in URIs with the
hexadecimal representation of `%26'.


I tried using srch.split("%26 ") but that would not split on an actual
question mark, only the literal string "%26".


Of course. You need to understand the difference between special
characters used as special characters, and special characters used
as ordinary characters within URI components. The latter needs to
be escaped, the former should not be escaped.
It finally turns out that what I need is to split on "\?".
"\?" is semantically equal to "?" since there is no such escape sequence
in _string_ literals. And no, you should not need to split on `?'. If
you need a *literal* `?' character, you need to escape it then in the URI.
var srch = window.location .search.substri ng(1);
Location is a host object and in some implementations the question
mark character is not part of location.search , so you need to check
for it before processing. And for compatibility reasons you should
use location.search , not window.location .search.
var parts = srch.split("\?" );
See above.
for (var i = 0; i < parts.length; i++) {
var temp = parts[i].split("=");
`temp' is redefined on every loop which will yield a warning in Mozilla/5.0.

Use

var temp;
for (...)
{
// ...
temp = ...
// ...
}

instead.
switch (temp[0]) {
case "xx": xx = +temp[1]; break;
case "yy": yy = +temp[1]; break;
case "ff": ff = +temp[1]; break;
case "ll": level = +temp[1]; break;
}
}

Does this look good?


Quite. Using properties of a container object instead of switch...case
and global variables would look even better. Why reinventing the wheel,
check out JSX:search.js, it's for free. The only thing you are required
to respect is the GPL. And please copy and distribute it then.
HTH

PointedEars
Jul 20 '05 #10

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

Similar topics

8
9436
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $ Last-Modified: $Date: 2003/10/28 19:48:44 $ Author: A.M. Kuchling <amk@amk.ca> Status: Draft Type: Standards Track
2
3946
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home Canonicalpath-Directory4: \\wkdis3\ROOT\home\bwe\ You selected the file named AAA.XML getXmlAlgorithmDocument(): IOException Not logged in
16
2878
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after the preload has been completed.
0
4117
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We have an schema (s1) on an Oracle 9i database with database links pointing to a schema (s2) on another Oracle 9i database.
9
4054
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics" portions of Babe Ruth page (http://www.baseballprospectus.com/dt/ruthba01.shtml) and store that info in a CSV file. Also, I would like to do this for numerous players whose IDs I have stored in a text file (e.g.: cobbty01, ruthba01, speaktr01, etc.)....
5
4297
by: randy | last post by:
Can some point me to a good example of parsing XML using C# 2.0? Thanks
3
4373
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in the file) with the location. And for a particular section I parse only that section. The file is something like, .... DATAS
13
4489
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
7
2401
by: Daniel Fetchinson | last post by:
Many times a more user friendly date format is convenient than the pure date and time. For example for a date that is yesterday I would like to see "yesterday" instead of the date itself. And for a date that was 2 days ago I would like to see "2 days ago" but for something that was 4 days ago I would like to see the actual date. This is often seen in web applications, I'm sure you all know what I'm talking about. I'm guessing this...
1
4382
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or Stack code and parsing code my professor i does not like me using buffer reader on my code and my professor did even give me an example code for parsing as well as pop push top or Stack code and i don't know how to do this code into parsing and pop push...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.