URLdecode ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Andersen

    URLdecode ?

    Hi,

    Is there an equivalent to URLdecode() in javascript ?

    Details:
    In Asp.Net I am using
    HttpContext.Cur rent.Response.R edirect("generi cerror.htm?ErrS tr=" &
    Err.Number & ": " & Err.Description )

    In genericerror.ht m I have:

    <SCRIPT LANGUAGE="javas cript">
    var url = window.location .href;
    var urlPos = url.indexOf('?' ) + 1;
    var urlLength = url.length - urlPos;

    if (urlPos > 0) {
    url.substr(url, urlPos, urlLength);
    url.split('&');
    var values = url.split('&');
    value = values[0].split('=');
    document.write( value[1]);
    }
    </SCRIPT>

    But I have problems with whitespace (and perhaps later other "funny" chars)
    so how do I make the string "safe" or "back to normal" ?

    tia
    /jim


  • Andy Fish

    #2
    Re: URLdecode ?

    I beleive you want encodeURI and decodeURI (or maybe encodeURICompon ent and
    decodeURICompon ent)

    "Jim Andersen" <jim@officecons ult.dk> wrote in message
    news:bhvr7o$e9n $1@sunsite.dk.. .[color=blue]
    > Hi,
    >
    > Is there an equivalent to URLdecode() in javascript ?
    >
    > Details:
    > In Asp.Net I am using
    > HttpContext.Cur rent.Response.R edirect("generi cerror.htm?ErrS tr=" &
    > Err.Number & ": " & Err.Description )
    >
    > In genericerror.ht m I have:
    >
    > <SCRIPT LANGUAGE="javas cript">
    > var url = window.location .href;
    > var urlPos = url.indexOf('?' ) + 1;
    > var urlLength = url.length - urlPos;
    >
    > if (urlPos > 0) {
    > url.substr(url, urlPos, urlLength);
    > url.split('&');
    > var values = url.split('&');
    > value = values[0].split('=');
    > document.write( value[1]);
    > }
    > </SCRIPT>
    >
    > But I have problems with whitespace (and perhaps later other "funny"[/color]
    chars)[color=blue]
    > so how do I make the string "safe" or "back to normal" ?
    >
    > tia
    > /jim
    >
    >[/color]


    Comment

    • Jim Andersen

      #3
      Re: URLdecode ?

      kaeli wrote:[color=blue]
      > In article <bhvr7o$e9n$1@s unsite.dk>, jim@officeconsu lt.dk enlightened
      > us with...[color=green]
      >> But I have problems with whitespace (and perhaps later other "funny"
      >> chars) so how do I make the string "safe" or "back to normal" ?
      >>[/color]
      >
      > var x = unescape(url);[/color]

      thx !

      Along the same lines...

      In .NET I use RegisterClientS criptBlock as a kind of msgbox.
      msgStr="Hello"
      pageref.Registe rClientScriptBl ock("aaa", "<script language=JavaSc ript>
      alert('" & msgstr & "')</SCRIPT>")

      works great. BUT, not if msgStr="Hel'lo" (with a single quote). And the
      content of msgStr is of course unknown to me at runtime.

      Whats the syntax for escaping msgstr ?

      tia
      /jim



      Comment

      • kaeli

        #4
        Re: URLdecode ?

        In article <bi2c0m$dqj$1@s unsite.dk>, jim@officeconsu lt.dk enlightened
        us with...[color=blue]
        > In .NET I use RegisterClientS criptBlock as a kind of msgbox.
        > msgStr="Hello"
        > pageref.Registe rClientScriptBl ock("aaa", "<script language=JavaSc ript>
        > alert('" & msgstr & "')</SCRIPT>")
        >
        > works great. BUT, not if msgStr="Hel'lo" (with a single quote). And the
        > content of msgStr is of course unknown to me at runtime.
        >
        > Whats the syntax for escaping msgstr ?
        >[/color]

        Technically? A backslash before any quotes. That's the hard way, though.
        Make the single quotes into double quotes and escape them so they don't
        mess up your VB string. Then you won't have to worry about single
        quotes, but double quotes will still mess you up.
        In VB, IIRC, to escape a double quote literal you preface it with
        another double quote. In javascript, you use a backslash.

        alert(""" & msgstr & """)</SCRIPT>

        You could always parse msgstr and put a single backslash before any
        quotes, but I think that's overcomplicatin g things if all you need to
        worry about are single quotes.


        -------------------------------------------------
        ~kaeli~
        Press any key to continue or any other key to quit.
        Who is General Failure and why is he reading
        my hard disk?


        -------------------------------------------------

        Comment

        • Jim Andersen

          #5
          Re: URLdecode ?

          kaeli wrote:[color=blue][color=green]
          >> Whats the syntax for escaping msgstr ?[/color][/color]
          [color=blue]
          > In javascript, you use a backslash.
          >
          > alert(""" & msgstr & """)</SCRIPT>
          >
          > You could always parse msgstr and put a single backslash before any
          > quotes, but I think that's overcomplicatin g things if all you need to
          > worry about are single quotes.[/color]

          yes... but is that all I need to worry about ? What else could freak that
          javascript out of working ?

          /jim


          Comment

          • kaeli

            #6
            Re: URLdecode ?

            In article <bi2iu3$shr$2@s unsite.dk>, jim@officeconsu lt.dk enlightened
            us with...[color=blue][color=green]
            > >
            > > alert(""" & msgstr & """)</SCRIPT>
            > >
            > > You could always parse msgstr and put a single backslash before any
            > > quotes, but I think that's overcomplicatin g things if all you need to
            > > worry about are single quotes.[/color]
            >
            > yes... but is that all I need to worry about ? What else could freak that
            > javascript out of working ?
            >[/color]

            Just having embedded double quotes, AFAIK.

            I don't think any other normal characters would be a problem. I assume
            since you have control of the string, no really odd characters (like
            non-printing chars or foreign chars) would be entered.

            -------------------------------------------------
            ~kaeli~
            Press any key to continue or any other key to quit.
            Who is General Failure and why is he reading
            my hard disk?


            -------------------------------------------------

            Comment

            • Jim Andersen

              #7
              Re: URLdecode ?

              kaeli wrote:[color=blue]
              > In article <bi2iu3$shr$2@s unsite.dk>, jim@officeconsu lt.dk enlightened
              > us with...[color=green][color=darkred]
              >>>
              >>> alert(""" & msgstr & """)</SCRIPT>
              >>>
              >>> You could always parse msgstr and put a single backslash before any
              >>> quotes, but I think that's overcomplicatin g things if all you need
              >>> to worry about are single quotes.[/color]
              >>
              >> yes... but is that all I need to worry about ? What else could freak
              >> that javascript out of working ?
              >>[/color]
              >
              > Just having embedded double quotes, AFAIK.
              >
              > I don't think any other normal characters would be a problem. I assume
              > since you have control of the string, no really odd characters (like
              > non-printing chars or foreign chars) would be entered.[/color]

              I actually do
              msgstr=err.Numb er & ": " & err.Description
              and once .Description was
              no such object 'zzzTblTst'
              I am gonna experiment with server.getlaste rror etc, and that could (AFAIK)
              include both [ and & and { etc.

              /jim


              Comment

              • Jim Andersen

                #8
                Re: URLdecode ?

                >> But I have problems with whitespace (and perhaps later other "funny"
                chars)[color=blue][color=green]
                >> so how do I make the string "safe" or "back to normal" ?[/color][/color]
                [color=blue]
                > var x = unescape(url);[/color]

                hmmm, nope.

                Have searched hi&lo on google and it seems it's a known issue. .Net and Java
                doesn't use the same algorithm for encoding-decoding. So I don't get my
                original string.

                /jim


                Comment

                • Jim Ley

                  #9
                  Re: URLdecode ?

                  On Thu, 4 Sep 2003 16:54:20 +0200, "Jim Andersen"
                  <jim@officecons ult.dk> wrote:
                  [color=blue]
                  >Have searched hi&lo on google and it seems it's a known issue. .Net and Java
                  >doesn't use the same algorithm for encoding-decoding. So I don't get my
                  >original string.[/color]

                  decodeURICompon ent

                  Jim.
                  --
                  comp.lang.javas cript FAQ - http://jibbering.com/faq/

                  Comment

                  • Jim Andersen

                    #10
                    Re: URLdecode ?

                    Jim Ley wrote:[color=blue]
                    > On Thu, 4 Sep 2003 16:54:20 +0200, "Jim Andersen"
                    > <jim@officecons ult.dk> wrote:
                    >[color=green]
                    >> Have searched hi&lo on google and it seems it's a known issue. .Net
                    >> and Java doesn't use the same algorithm for encoding-decoding. So I
                    >> don't get my original string.[/color]
                    >
                    > decodeURICompon ent
                    >
                    > Jim.[/color]

                    So why isn't this working:

                    foo.aspx:
                    Response.Redire ct("tst.htm?msg =" & HttpUtility.Url Encode("6: division by
                    zero"))

                    tst.htm
                    <body>
                    Msg=
                    <SCRIPT LANGUAGE="javas cript">
                    var url = window.location .href;
                    var urlPos = url.indexOf('?' ) + 1;
                    var urlLength = url.length - urlPos;
                    if (urlPos > 0) {
                    url.substr(url, urlPos, urlLength);
                    url.split('&');
                    var values = url.split('&');
                    value = values[0].split('=');
                    document.write( value[1]);
                    }
                    </SCRIPT>
                    </body>

                    Result: I get Msg= 6%3a+division+b y+zero

                    I change tst.htm:
                    var=FunctionToG etQuerystring;
                    document.write( decodeURICompon ent(var))

                    Result: I get Msg= 6:+division+by+ zero

                    If I change foo.aspx:
                    Response.Redire ct("tst.aspx?ms g=" & HttpUtility.Url Encode("6: division by
                    zero"))

                    and have tst.aspx do:
                    Response.Write( "msg=" & HttpUtility.Url Decode(Request. QueryString("ms g")))
                    I finally get the desired result
                    msg=6: division by zero

                    regards
                    Jim Andersen


                    Comment

                    • Jim Ley

                      #11
                      Re: URLdecode ?

                      On Fri, 5 Sep 2003 12:10:18 +0200, "Jim Andersen"
                      <jim@officecons ult.dk> wrote:
                      [color=blue]
                      >foo.aspx:
                      >Response.Redir ect("tst.htm?ms g=" & HttpUtility.Url Encode("6: division by
                      >zero"))
                      >Result: I get Msg= 6:+division+by+ zero[/color]

                      It looks like your HttpUtility.Url Encode is not performing correct URI
                      encoding, it appears to be encoding space as +, this is not correct,
                      your problem is with that, not with javascript.

                      Jim.
                      --
                      comp.lang.javas cript FAQ - http://jibbering.com/faq/

                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: URLdecode ?

                        jim@jibbering.c om (Jim Ley) writes:
                        [color=blue]
                        > It looks like your HttpUtility.Url Encode is not performing correct URI
                        > encoding, it appears to be encoding space as +, this is not correct,[/color]

                        Well, for some definition of correct, it is. It is the same way form
                        data are encoded for the GET transfer method.

                        The solution is to change all plusses to spaces before decoding with
                        unescape:

                        var decoded = unescape(encode d.replace(/\+/g," "));

                        /L
                        --
                        Lasse Reichstein Nielsen - lrn@hotpop.com
                        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Jim Ley

                          #13
                          Re: URLdecode ?

                          On 05 Sep 2003 12:48:38 +0200, Lasse Reichstein Nielsen
                          <lrn@hotpop.com > wrote:
                          [color=blue]
                          >jim@jibbering. com (Jim Ley) writes:
                          >[color=green]
                          >> It looks like your HttpUtility.Url Encode is not performing correct URI
                          >> encoding, it appears to be encoding space as +, this is not correct,[/color]
                          >
                          >Well, for some definition of correct, it is. It is the same way form
                          >data are encoded for the GET transfer method.[/color]

                          True, but it is not URIEncoding which the method name suggests.

                          Jim.
                          --
                          comp.lang.javas cript FAQ - http://jibbering.com/faq/

                          Comment

                          • Jim Andersen

                            #14
                            Re: URLdecode ?

                            Lasse Reichstein Nielsen wrote:
                            [color=blue]
                            > The solution is to change all plusses to spaces before decoding with
                            > unescape:
                            >
                            > var decoded = unescape(encode d.replace(/\+/g," "));[/color]

                            Thx Lasse for the helpful answer (as opposed to Jim Ley's fantasies).

                            /jim


                            Comment

                            Working...