Eliminating double hyphens in Javascript?

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

I am having a problem where the original webpage has a double hyphen in the url (e.g. page--1--of--3.html ). Silly, I know - I can’t control the original page. The javascript escape function that I use does not go beyond a hyphen, and IIS 6 gives a file error not found in the service request resource.aspx?original=page--1--of--3.html

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

+4
source share
4 answers

Can you extend the evacuation function with some custom logic to manually encode hypen?

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

Something like that:

 function customEscape(url) { url = escape(url); url = url.replace(/-/g, '%2d'); return url; } location.href = customEscape("resource.axd?original=test--page.html"); 

Update for bookmarklet:

 <a href="javascript:location.href=escape('resource.axd?original=test--page.html').replace(/-/g, '%2d')">Link</a> 
+2
source

"escape" and "unescape" are deprecated because they do not encode all the corresponding characters. DO NOT USE ESCAPE or UNECCAPE . use "encodeURIComponent" and "decodeURIComponent" instead. It is supported in all but the oldest oldest browsers. This is truly a huge shame, this knowledge is not much more common.

(see also encodeURI and decodeURI)

edit: err is just checked, but it doesn't really overlap double hyphens. Unfortunately.

+4
source

You are doing something else wrong. - is legal at URLs and file names. Maybe the file is really not found?

+1
source

- used to comment on text in several scripting languages. SQL Server uses it to add comments. Do you use any database logic to store these file names? Or create any queries in which this name is part of the query string rather than using query parameters?

+1
source

Source: https://habr.com/ru/post/1286327/


All Articles