Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery encodeURI for href not working

I'm having problems encoding a string so I can place a variable into a link. I'm sure this is really simple, but i had trouble turning anything up.

$("a.inquiry").attr("href", "/inquiry/6933/text=" + encodeURI("text o"));

This doesn't work.

encodeURI("text o")

Still returns:

link/text o

Instead of:

link/text%20

Also tried:

$("a.inquiry").attr("href", encodeURIComponent("/inquiry/6933/text=" + "text o"));
like image 307
holden Avatar asked Sep 07 '10 15:09

holden


People also ask

Should I use encodeURI or encodeURIComponent?

If you have a complete URL, use encodeURI . But if you have a part of a URL, use encodeURIComponent .

What is the return value of encodeURI URI )?

Return valueA new string representing the provided string encoded as a URI.

Why is encodeURIComponent needed?

The encodeURIComponent function is used to encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two or three escape sequences representing the UTF-8 encoding of the character.

How do you decode or encode a URL in JavaScript?

1. decodeURI function: The decodeURI() function is used to decode URI generated by encodeURI(). Parameters: This function accepts a single parameter complete_encoded_uri_string which holds the encoded string. Return Value: This function returns the decoded string (original string).


1 Answers

Try with encodeURIComponent instead.

like image 157
Sarfraz Avatar answered Oct 05 '22 08:10

Sarfraz