Kāda ir atšķirība starp decodeURIComponent() un decodeURI() metodēm JavaScript?

Kada Ir Atskiriba Starp Decodeuricomponent Un Decodeuri Metodem Javascript



JavaScript ' decodeURIComponent() ” un „ decodeURI() ” metodes darbojas uz URI (Uniform Resource Identifier). Metode “decodeURIComponent()” atkodē tās komponentus, savukārt metode “decodeURI()” to atkodē pilnībā. Tādējādi to definīcijas skaidri nosaka atšķirību starp tām abām, taču tās atšķiras arī citu faktoru dēļ.

Šajā rokasgrāmatā ir aprakstīta atšķirība starp decodeURIComponent() un decodeURI() metodēm.

Vispirms apskatiet pamatus decodeURIComponent() un decodeURI metodes







pirms pāriet uz to atšķirībām.



Kāda ir atšķirība starp “decodeURIComponent()” un “decodeURI()” metodēm JavaScript?

Šajā sadaļā ir ietvertas būtiskās atšķirības starp “decodeURIComponent()” un “decodeURI()” metodēm:



Noteikumi decodeURIComponent() decodeURI()
Lietošana “decodeURIComponent()” tiek izmantots, lai atšifrētu URI komponentus, kas kodēti ar metodi “encodeURIComponent()”. “decodeURI()” tiek izmantots, lai atšifrētu pilnu URI, kas kodēts ar “encodeURI” metodes palīdzību.
Parametri “decodeURIComponent()” kā obligātu dekodēšanas parametru pieprasa “kodētu” URI, izmantojot metodi “encodeURIComponent()”. Lai veiktu dekodēšanas darbību, “decodeURI()” ir nepieciešams “kodēts” URI, izmantojot metodi “encodeURI()”.
Ierobežojums “decodeURIComponent()” atkodē tikai to URI daļu, kas atrodas starp “; / ? : @ & = + $ , #” atdalītāji. Tas šīs rakstzīmes uzskata par tekstu, nevis īpašām rakstzīmēm. “decodeURIComponent()” atšifrē visu URI.

Tagad aplūkojiet norādītās atšķirības praktiski.





1. atšķirība: “decodeURIComponent()” un “decodeURI()” metožu piemērošana, pamatojoties uz “izmantošanu”

Saskaņā ar pirmo atšķirību metode “decodeURIComponent()” atkodē kodēto virkni, izmantojot metodi “encodeURIComponent()”, un metode “decodeURI()” atkodē kodēto virkni, izmantojot metodi “encodedURI()”.

JavaScript kods

< skripts >

var uri = '%3B%2C%41%3F%3A%41%26%3D'

var encoded_URI = encodeURIComponent ( veids ) ;

konsole. žurnāls ( 'Izvade(decodeURIComponent()):' + decodeURIComponent ( veids ) )

var encoded_uri = encodeURI ( veids ) ;

konsole. žurnāls ( 'Dekodēts URI:' + decodeURI ( encoded_uri ) ) ;

skripts >

Iepriekš minētajā koda fragmentā:



  • Mainīgais “uri” inicializē URI (uniform Resource Identifier).
  • Mainīgais “encoded_URI” piemēro “ encodeURIComponent() ” metodi, lai kā parametru kodētu nodoto “uri”.
  • Pēc tam metode “console.log()” izmanto “ decodeURIComponent() ” metodi, lai atšifrētu nesen kodēto virkni un pēc tam parādītu to konsolē.
  • Pēc tam ' decodeURI() ” metode atkodē kodēto URI, izmantojot encodeURI() ”, kas norādīta mainīgajā “encoded_uri”.

Izvade

Ir redzams, ka konsole parāda dekodētos URI, izmantojot metodes “decodeURIComponent()” un “decodeURI()”.

2. atšķirība: “decodeURIComponent()” un “decodeURI()” metožu izmantošana, pamatojoties uz “parametriem”

No “1. atšķirības” var skaidri redzēt, ka “decodeURIComponent()” pieņem kodēto URI, izmantojot metodi “encodeURIComponent()”, un “decodeURI()” darbojas uz kodētā URI, izmantojot metodi “encodeURI()”, kā parametrs.

3. atšķirība: “decodeURIComponent()” un “decodeURI()” metožu izmantošana, pamatojoties uz “dekodētām rakstzīmēm”

Trešo atšķirību starp “decodeURIComponent()” un “decodeURI()” metodēm var analizēt, dekodējot konkrēto URI. Apskatīsim to praktiski.

JavaScript kods

< skripts >

var uri = '@%20&%7F=%3A%41%26%3D'

konsole. žurnāls ( 'Izvade(decodeURIComponent()):' + decodeURIComponent ( veids ) )

konsole. žurnāls ( 'Izvade(decodeURI()):' + decodeURI ( veids ) )

skripts >

Dotajā skripta sadaļā:

  • Pirmais ' console.log() metode parāda dekodēto URI, izmantojot decodeURIComponent() ” metode.
  • Otrā metode “console.log()” parāda dekodēto URI, izmantojot “ decodeURI() ” metode.

Izvade

Konsole skaidri parāda, ka metode “decodeURIComponent()” atkodē tikai URI komponentu, kas atrodas starp rakstzīmēm “@ & =”, nevis visu URI.

Secinājums

JavaScript ' decodeURIComponent() ” un „ decodeURI() ” metodes atšķiras viena no otras atkarībā no lietojums ”, “ parametrus ”, un “ ierobežojums ” faktori. Galvenā/būtiskā atšķirība savā starpā ir tāda, ka “decodeURIComponent()” atkodē URI komponentu, savukārt metode “decodeURI()” atkodē visu URI. Šī rokasgrāmata praktiski izskaidro galvenās atšķirības starp decodeURIComponent() un decodeURI() metodēm.