“Urlencode JavaScript” Kod odpowiedzi

Urlencode JavaScript

var url = encodeURIComponent('url');
QP2

JS urlencode

/* There are 11 characters which are not encoded by encodeURI, 
but encoded by encodeURIComponent. */

encodeURI("test uri"); 
// "test%20uri"
encodeURI("test uri"); 
// "test%20uri"

encodeURI("test uri/with a slash"); 
// "test%20uri/with%20a%20slash"
encodeURIComponent("test uri/with a slash"); 
// "test%20uri%2Fwith%20a%20slash"
Frasween

Parametr JS Escape URL

var myUrl = "http://www.image.com/?username=unknown&password=unknown";
var encodedURL= "http://www.foobar.com/foo?imageurl=" + encodeURIComponent(myUrl);
GutoTrosla

JavaScript HTML Encode

function htmlEntities(s){
	return s.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
  		return '&#' + i.charCodeAt(0) + ';';
	});
}
Friendly Hawk

Encodeuricomponent JS

var set1 = ";,/?:@&=+$";  // Reserved Characters
var set2 = "-_.!~*'()";   // Unescaped Characters
var set3 = "#";           // Number Sign
var set4 = "ABC abc 123"; // Alphanumeric Characters + Space

console.log(encodeURI(set1)); // ;,/?:@&=+$
console.log(encodeURI(set2)); // -_.!~*'()
console.log(encodeURI(set3)); // #
console.log(encodeURI(set4)); // ABC%20abc%20123 (the space gets encoded as %20)

console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24
console.log(encodeURIComponent(set2)); // -_.!~*'()
console.log(encodeURIComponent(set3)); // %23
console.log(encodeURIComponent(set4)); // ABC%20abc%20123 (the space gets encoded as %20)

Abhi the Googler

Koduj adres URL w JavaScript?

Check out the built-in function encodeURIComponent(str) and encodeURI(str).
In your case, this should work:

var myOtherUrl = 
       "http://example.com/index.html?url=" + encodeURIComponent(myUrl);
shafeeque

Odpowiedzi podobne do “Urlencode JavaScript”

Pytania podobne do “Urlencode JavaScript”

Więcej pokrewnych odpowiedzi na “Urlencode JavaScript” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu