Missing slashes when using javascript:location.href=

Posted on 2006-05-13
Last Modified: 2008-01-09
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,
Question by:PvBredow
    LVL 49

    Expert Comment

    The \-slash is an escape character in javascript. You either have to use \\ or use /, which has no special meaning to javascript inside strings.


    Author Comment

    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)

    LVL 49

    Expert Comment

    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?

    LVL 5

    Expert Comment


    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.


    Author Comment

    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.

    Author Comment

            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.
    LVL 17

    Expert Comment

    Why dont you try with the other slashes :
    location.href=myurl + '../Source Files/Analyst.mht'">Analyst: Job Roles</href>
    LVL 5

    Accepted Solution

    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.


    LVL 30

    Expert Comment

    > 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.
    LVL 1

    Expert Comment

    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>


    Author Comment

    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 Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    "I want to put my photos online, but I don't want them stolen.  What settings should I use?" When You Put Photos Online First and foremost, any digital file published on the WWW can be copied, stored, modified, retransmitted, etc.  Remember Naps…
    This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
    In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
    The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now