views:

570

answers:

4

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

A: 

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

\#
StingyJack
Just tried that. The url called is then gets closer, but still fails. Updating the question
shrub34
A: 

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

Kon
+5  A: 

You seek the dark magicks of encodeURI:

window.open("http://your-url.com/" + encodeURIComponent("foo#123.jpg"));
Rahul
This did it. Not sure why this wasn't working for us earlier. Thanks.
shrub34
Figured out why it wasn't working earlier. Needed to first open the window then on the returned object change the location.href to the escaped string.
shrub34
A: 

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>
Claudio
The not escaping is no good since the URL stops at the # symbol due to it being a special URL character
shrub34