url parsing

F

Fabian

I use the following to parse the url

var srch = window.location.search.substring(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.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?
 
F

Fabian

Fabian hu kiteb:
I use the following to parse the url

var srch = window.location.search.substring(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.
 
L

Lasse Reichstein Nielsen

Fabian said:
I use the following to parse the url

var srch = window.location.search.substring(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, "&", 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
 
F

Fabian

Lasse Reichstein Nielsen hu kiteb:
That depends.

If the code is embedded in an HTML page, then the ampersand should be
escaped (as an HTML entity, "&", 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, , &, and \u0026. I'm not aware of other methods for
escaping the character.
 
T

Thomas 'PointedEars' Lahn

Fabian said:
var srch = window.location.search.substring(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.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
 
T

Thomas 'PointedEars' Lahn

Fabian said:
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 `&' (case-insensitive) and
`&' would represent the same as the former.

http://www.w3.org/TR/html4/charset.html#h-5.3.1


PointedEars
 
F

Fabian

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.
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.substring(1);
var parts = srch.split("\?");
for (var i = 0; i < parts.length; i++) {
var temp = parts.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?
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
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.prototype, this is
equivalent to
for (var i in parts)

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
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.
So unless you have extended Array.prototype or Object.prototype, this is
equivalent to
for (var i in parts)

It is not since the order of the retrieved properties is
then undefined.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Fabian said:
Thomas 'PointedEars' Lahn hu kiteb:

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.substring(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.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
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Fabian wrote:
for (var i = 0; i < parts.length; i++) {
var temp = parts.split("=");


`temp' is redefined on every loop which will yield a warning in Mozilla/5.0.


Are you sure. I don't get any warning in Mozilla Firebird 0.7.

I believe that according to ECMA262, a variable declaration only generates
a local variable once, even if inside a loop. So the code following examples
are completely equivalent:

---
for (var i=0;i<4;i++) {
var temp = i;
}
 
F

Fabian

Thomas 'PointedEars' Lahn hu kiteb:
... 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.

The GPL is why I won't use it, but thanks for offering.
 
J

Jim Ley

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.

Fully agree with not re-inventing the wheel, but I'd strongly
recommend finding something other than a GPL solution, all you script
would then have to be GPL'd.

Jim.
 
T

Thomas 'PointedEars' Lahn

Fabian said:
Thomas 'PointedEars' Lahn hu kiteb:
... 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.

The GPL is why I won't use it, [...]

What exactly are you afraid of?
but thanks for offering.

You are welcome.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Jim said:
Fully agree with not re-inventing the wheel, but I'd strongly
recommend finding something other than a GPL solution, all you script
would then have to be GPL'd.

And the problem is?


PointedEars
 
J

Jim Ley

And the problem is?

The problem with GPL?

|b) You must cause any work that you distribute or publish, that in whole
|or in part contains or is derived from the Program or any part thereof, to
|be licensed as a whole at no charge to all third parties under the terms
|of this License.

So if I use a bit of GPL in my script, the whole script becomes GPL'd

|c) If the modified program normally reads commands interactively when
|run, you must cause it, when started running for such interactive use
|in the most ordinary way, to print or display an announcement
|including an appropriate copyright notice and a notice that there is
|no warranty (or else, saying that you provide a warranty) and that
|users may redistribute the program under these conditions, and
| telling the user how to view a copy of this License. (Exception:
| if the Program itself is interactive but does not normally print such
|an announcement, your work based on the Program is not
|required to print an announcement.)

Which is rather alarming (you might be able to argue the snippet was
interactive but didn't print an announcement, but I think that would
be a struggle in many cases.)

basically the GPL makes it hard to sell a whole work, even if only 1%
of your codebase is GPL, then you have to give everyone a RF licence.
I don't like this, I don't feel it's fair.

All my sourcecode that is not copyright someone else, is under a
modified BSD licence, this is much friendlier, and I would encourage
others to use the same or similar if they want an Open source licence.

Jim.
 
F

Fabian

Thomas 'PointedEars' Lahn hu kiteb:
Fabian said:
Thomas 'PointedEars' Lahn hu kiteb:
... 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.

The GPL is why I won't use it, [...]

What exactly are you afraid of?

Kind of weird to explain. For me, javascript and html is a hobby. I get
more pleasure out of the process of building the stuff than from
actually having the completed thing in front of me. Taking smething
pre-built wholesale defeats both of those goals. It also means I don't
understand the code I end up using so well. I appreciate pointers and
advice; the one thing I don't want is for the answer to be given to me.

I also have this mad dream that one day I may be able to use some of my
code
on a commercial basis, which would also ot be possible with GPL code.
 
T

Thomas 'PointedEars' Lahn

Jim said:
The problem with GPL?

|b) You must cause any work that you distribute or publish, that in whole
|or in part contains or is derived from the Program or any part thereof, to
|be licensed as a whole at no charge to all third parties under the terms
|of this License.

You destroyed the context. It reads:

| When we speak of free software, we are referring to freedom, not
| price. Our General Public Licenses are designed to make sure that you
| have the freedom to distribute copies of free software (and charge for
| this service if you wish), that you receive source code or can get it
| if you want it, that you can change the software or use pieces of it
| in new free programs; and that you know you can do these things.
| [...]
|
| TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
| [...]
| 2. You may modify your copy or copies of the Program or any portion
| of it, thus forming a work based on the Program, and copy and
| distribute such modifications or work under the terms of Section 1
| above, provided that you also meet all of these conditions:
|
| [...]
| b) You must cause any work that you distribute or publish, that in
| whole or in part contains or is derived from the Program or any
| part thereof, to be licensed as a whole at no charge to all third
| parties under the terms of this License.

It means that the license must be free of charge, not the program.
So if I use a bit of GPL in my script, the whole script becomes GPL'd

If used in a whole, yes.
|c) If the modified program normally reads commands interactively when
|run, you must cause it, when started running for such interactive use
|in the most ordinary way, to print or display an announcement
|including an appropriate copyright notice and a notice that there is
|no warranty (or else, saying that you provide a warranty) and that
|users may redistribute the program under these conditions, and
| telling the user how to view a copy of this License.(Exception:
| if the Program itself is interactive but does not normally print such
|an announcement, your work based on the Program is not
|required to print an announcement.)

Which is rather alarming (you might be able to argue the snippet was
interactive but didn't print an announcement, but I think that would
be a struggle in many cases.)

The above section obviously does not apply for JavaScript scripts.
basically the GPL makes it hard to sell a whole work,

It does not.
even if only 1% of your codebase is GPL, then you have to give everyone
a RF licence.

That's the idea. But they are not allowed to sell work based
on your ideas without mentioning your co-authorship.
I don't like this, I don't feel it's fair.

What is not fair about it? That others can use your work and
can modify it, provided that they still mention your authorship?
You got the wrong idea about free software.
I would encourage others to use the same or similar if they want an
Open source licence.

There is a difference between free software and Open Source software.
That difference is freedom. http://www.fsf.org/philosophy/free-sw.html


PointedEars
 
T

Thomas 'PointedEars' Lahn

Fabian said:
Thomas 'PointedEars' Lahn hu kiteb:
Fabian said:
The GPL is why I won't use it, [...]

What exactly are you afraid of?

Kind of weird to explain. For me, javascript and html is a hobby. I get
more pleasure out of the process of building the stuff than from
actually having the completed thing in front of me. Taking smething
pre-built wholesale defeats both of those goals. It also means I don't
understand the code I end up using so well. I appreciate pointers and
advice; the one thing I don't want is for the answer to be given to me.

I can accept that.
I also have this mad dream that one day I may be able to use some of my
code on a commercial basis, which would also ot be possible with GPL code.

I cannot accept that since it is of course wrong.
Read http://www.fsf.org/philosophy/free-sw.html


PointedEars
 
J

Jim Ley

Jim Ley wrote:
| b) You must cause any work that you distribute or publish, that in
| whole or in part contains or is derived from the Program or any
| part thereof, to be licensed as a whole at no charge to all third
| parties under the terms of this License.

It means that the license must be free of charge, not the program.

What? So you're saying I can sell copies of GPL software as long as I
don't sell the licence, just the software itself. (which I can't of
course sell as I don't own the parts I didn't write, I only have a
licence to use it...) I think you should consult a lawyer...
If used in a whole, yes.

How can it be used in a part but not the whole, please explain?
The above section obviously does not apply for JavaScript scripts.

Please cite legal opinion on this, preferably in a UK or European
court. I'm afraid I see nothing in the GPL which exclued javascript
from any part of it.
It does not.

Could you explain how I can achieve it then?
That's the idea. But they are not allowed to sell work based
on your ideas without mentioning your co-authorship.

That sounds like a BSD licence to me, not a GPL one.
What is not fair about it? That others can use your work and
can modify it, provided that they still mention your authorship?
You got the wrong idea about free software.

Nope, that's a BSD (or similar) licence, the GPL licence requires that
my modifications/additions etc. are also GPL'd, that's restrictive.
There is a difference between free software and Open Source software.
That difference is freedom. http://www.fsf.org/philosophy/free-sw.html

What are you talking about? the BSD licence is far less restrictive
than the GPL, if you also read further than the page you cited, you'll
see that the BSD is fully compatible with the GPL...

http://www.fsf.org/licenses/license-list.html

Jim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top