admin管理员组

文章数量:1392086

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' bees '%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

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' bees '%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

Share Improve this question edited Oct 24, 2008 at 15:31 munity wiki
2 revs
shrub34 1
  • Can you give an example of the code? – StingyJack Commented Oct 24, 2008 at 15:27
Add a ment  | 

4 Answers 4

Reset to default 6

You seek the dark magicks of encodeURI:

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

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>

本文标签: url rewritingjavascript windowopen() andsymbolStack Overflow