views:

432

answers:

4

I have a Javascript bookmarklet that, when clicked on, redirects the user to a new webpage and supplies the URL of the old webpage as a parameter in the query string.

I'm running into a problem when the original webpage has a double hyphen in the URL (ex. page--1--of--3.html). Stupid, I know - I can't control the original page The javascript escape function I'm using does not escape the hyphen, and IIS 6 gives a file not found error if asked to serve resource.aspx?original=page--1--of--3.html

Is there an alternative javascript escape function I can use? What is the best way to solve this problem? Does anybody know why IIS chokes on resource.aspx?original=page--1 and not page-1?

+1  A: 

Can you expand the escape function with some custom logic to encode the hypen's manually?

resource.aspx?original=page%2d%2d1%2d%2dof%2d%2d3.html

Something like this:

function customEscape(url) {
    url = escape(url);
    url = url.replace(/-/g, '%2d');
    return url;
}

location.href = customEscape("resource.axd?original=test--page.html");

Update, for a bookmarklet:

<a href="javascript:location.href=escape('resource.axd?original=test--page.html').replace(/-/g, '%2d')">Link</a>
bendewey
Is it possible to do that within a bookmarklet?
amdfan
I added an update with a bookmarklet
bendewey
I accept that this works but frankly it makes no sense. This would imply that IIS6 does something to URL's that isn't in the spec (not surprising but i've never heard of it either).
SpliFF
-1. Don't try to make your own escape function, you'll never be able to cover all the corner cases. There's built in functions that deal with this stuff already. See my answer for more.
Breton
SpliFF - I'm with you! This is on a shared host server though - the only explanation I can think of is that the company has configured the IIS server in a way that has this bizarre side effect.
amdfan
A: 

You're doing something else wrong. -- is legal in URLs and filenames. Maybe the file really isn't found?

SpliFF
I'm quite certain the file exists and is there. The error only occurs when the double hyphens are present. Bear in mind this is IIS 6, which may account for that.
amdfan
have you checked that resource.aspx isn't stripping out double-hythens for some crazy reason?
SpliFF
yes, quite sure - the server returns a 404 before resource.aspx even sees the request
amdfan
A: 

-- is used to comment out text in a few scripting languages. SQL Server uses it to add comments. Do you use any database logic to store those filenames? Or create any queries where this name is part of the query string instead of using query parameters?

Workshop Alex
no and no. Nice thinking though.
amdfan
+1  A: 

"escape" and "unescape" are deprecated precisely because it doesn't encode all the relevant characters. DO NOT USE ESCAPE OR UNESCAPE. use "encodeURIComponent" and "decodeURIComponent" instead. Supported in all but the oldest most decrepit browsers. It's really a huge shame this knowledge isn't much more common.

(see also encodeURI and decodeURI)

edit: err just tested, but this doesn't really cover the double hyphens still. Sorry.

Breton
These don't seem to escape hyphens...
Alconja
you're right, sorry- But the basic thrust of the message is still true, even if it doesn't solve the problem at hand.
Breton