Question

I am trying to link to a file that has the '#' character in via a window.open() call. The file does exist and can be linked to just fine using a normal anchor tag.

I have tried escaping the '#' character with '%23' but when the window.open(myurl) gets processed, the '%23' becomes '%2523'. This tells me that my url string is being escapped by the window.open call changing the '%' to the '%25'.

Are there ways to work around this extra escaping.

Sample code:

<script language="javascript">
function escapePound(url)
{
   // original attempt
   newUrl = url.replace("#", "%23");
   // first answer attempt - doesn't work
   // newUrl = url.replace("#", "\\#");

   return newUrl;
 }
</script>
<a href="#top" onclick="url = '\\\\MyUNCPath\\PropertyRushRefi-Add#1-ABCDEF.RTF'; window.open(escapePound(url)); return true;">Some Doc</a>

URL that yells says "file://MyUNCPath/PropertyRushRefi-Add%25231-ABCDEF.RTF" cannot be found

Was it helpful?

Solution

You seek the dark magicks of encodeURI:

window.open("http://your-url.com/" + encodeURIComponent("foo#123.jpg"));

OTHER TIPS

Did you try using the standard text escape char "\"?

\#

Have you tried URL encoding via JavaScript as done here and here?

Have you tried not escaping the url?

<a href="#top onclick="url = '\\\\MyUNCPath\\PropertyRushRefi-Add#1-ABCDEF.RTF'; window.open(url); return true;">Some Doc</a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top