[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 708
  • Last Modified:

Missing slashes when using javascript:location.href=

I am using the following line on my page:

<a href="javascript:location.href=myurl + '..\Source Files\Analyst.mht'">Analyst: Job Roles</href>

myurl is a script variable that contains the name of a ASP page and the beginning of a querystring.
I want to add something to the back of the querystring (in this case '..\Source Files\Analyst.mht')
I have experimented with this before, and it has always worked until now.      The problem I am seeing is that by the time the querystring reaches my ASP, the last part of the query string has been converted to '..Source FilesAnalyst.mht'
(Notice that both the slashes have been removed).

I am trying to pass the name of the selected file to the ASP for logging, where a redirect will then open the page the user has requested.       If  I can't find a way to pass the slashes, this won't work.

As an experiment, I tried doubling the slashes (similiar to how you often need to deal with quotes), but that didn't help.
Can anyone explain what is happening, and give me an example of the syntax that will fix this?

In case it is relevant, the above line is from the client side HTML page, and I am using Internet Explorer. (This is for a corporate interal web where only IE is supported)

              thanks very much,
  • 4
  • 2
  • 2
  • +3
1 Solution
The \-slash is an escape character in javascript. You either have to use \\ or use /, which has no special meaning to javascript inside strings.

PvBredowAuthor Commented:
Thanks for the suggestion.       Converting single \'s to \\'s helps produce what appears to be the correct querystring, but somehow it is still not working.       I had a response.redirect on my ASP, which would work if the file name was passed normally, but now fails.

I mean that:
<a href="javascript:location.href=myurl + '..\\Source Files\\Equity Sales.mht'"  title="Equity Sales: Job Roles">Equity Sales: Job Roles</href>

doesn't work, but:

<a href="javascript:location.href=..\Source Files\Equity Sales.mht"  title="Equity Sales: Job Roles"
>Equity Sales: Job Roles</href>

did previously work.

(I am getting more than a little irritated with these syntax wars I am fighting)

Well, for urls, it is often better to use / rather than \\ that saves you a lot of trouble, even on windows servers.

what does myurl do/contain?

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


The problem is that "myurl" is a javscript variable and cannot be accessed using tthis syntax. Instead try changing the format to

<a href="Javacript:void();" onClick="Redirect();" title="Equity Sales: Job Roles"
>Equity Sales: Job Roles</href>

and in the script add this function

   var location = myurl + escape("..\Source Files\Analyst.mht");
   this.location.href = location;

Hope this helps.

PvBredowAuthor Commented:
myurl is a script variable containing the name of my ASP, and the beginning of the querystring.        the reason I have done it that way, instead of hardcoding the name of the ASP into every href is that I want to pass windows user name as part of the query string.        On my client side page, i have a script that reads the windows user name from the registry, and adds it to 'myurl'.

essentially, myurl is:

http://myserver/submit.asp?user=Pvbredow&sfile=..\Source Files\userfile.htm

where    ..\Source Files\userfile.htm    is the name of the file the user wants to see, and Pvbredow in this case represents the windows user name.

Perhaps I am just being stubborn about this, but I find it hard to believe there is no syntax that can handle this.
PvBredowAuthor Commented:
        Actually, I am using VbScript, not javascript.       I was advised by someone else in this forum that I could use the line
a href="javascript:location.href=myurl + '..\\Source Files\\Equity Sales.mht'"

to concatenate the contents of the VbScript variable 'myurl' with a text string.        Actually, this has seemed to work quite well, until I hit the issue that the string I want to concatenate contained slashes.
Next, on further advice, I doubled the slashes, which seemed to achieve getting the correct querystring to my ASP page, but somehow at that point the response.redirect on my ASP stopped working.

I wondered if some non-printing characters were causing the problem, but checking the length of the received string in my ASP confirmed that the characters were the correct length, and certainly appear to be correct when written to a log file.

I'm sorry if my specs are only coming out in bits and pieces, which must be confusing.

I am trying to produce client side HTML pages that contain a script to get the user name from the registry, build a querystring containing the name of the user & the name of the requested file, and pass that to my ASP.
The ASP writes to a log file, and then uses a redirect so the user sees the file they wanted to see.

Somehow, all the parts of this work in separate stages.         I can't hardcode the path to my source files in my ASP since different files are stored in my different directories.

I appreciate the help so far, hopefully a solution isn't far away.
Why dont you try with the other slashes :
location.href=myurl + '../Source Files/Analyst.mht'">Analyst: Job Roles</href>
Try using escape() function for the '..\\Source Files\\Analyst.mht' before appending it to the myurl because we generally don't send url as plain text in parameters.


> Try using escape()
I agree with that.

escape() encodes all the characters that are not url-safe (equivalent to url_encode in php).
It will encode your white space into %20 and your backslash into %5C.

The resulting url should be:

You'll then need to unescape the file path on the server-side before you use it.
Hi PvBredow,

I think your problem is quite easier than you think.

When you give a location you can only choose between giving a absolute path and a referential path.
In your href, you specify a absolute patch in MyUrl and a referential path in '..\\Source Files\\Analyst.mht'.
If you delete the referential part '..' from you string and place forward slashes in your string, your code should work.

The following code should solve your problem:

<a href="javascript:location.href=myurl + '/Source Files/Analyst.mht'">Analyst: Job Roles</href>

PvBredowAuthor Commented:
Thanks, it is working now.     Sorry for the delay in giving you your points, but I had tried earlier to do so, and it seems it failed, since this question is still marked as open.       The solution ultimately was to use 'escape', so the points go to ARUNRS, although i would like to thank all the rest of you who contributed suggestions.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now