An warware: javascript sami url na yanzu

Babban matsalar ita ce "URL na yanzu" a cikin JavaScript ba koyaushe abin dogaro bane. Misali, idan ka loda shafi a cikin mai binciken gidan yanar gizo, URL na yanzu zai zama adireshin shafin da kansa. Idan ka loda shafin ta amfani da wani mashigin bincike daban, ko kuma idan ka loda shi daga fayil akan kwamfutarka, URL ษ—in na yanzu na iya bambanta.

var currentURL = window.location.href;

Wannan layin lambar yana bayyana maษ“alli da ake kira "currentURL" kuma ya sanya masa ฦ™imar URL ษ—in shafin yanar gizon yanzu.

samu halin yanzu Properties

Akwai ฦดan hanyoyi don samun abubuwan mallakar abu na yanzu a JavaScript. Hanya ษ—aya ita ce amfani da hanyar Object.getOwnPropertyNames(). Wannan hanyar tana dawo da jerin duk abubuwan mallakar abu. Hakanan zaka iya amfani da sunan kadarorin azaman mai canzawa don samun damar ฦ™imar kadarorin.

Wata hanya don samun abubuwan da ke cikin abu na yanzu shine amfani da hanyar Object.keys(). Wannan hanyar tana dawo da jerin duk maษ“allan (ko masu ganowa na musamman) na wani abu. Hakanan zaka iya amfani da sunan maษ“alli azaman mai canzawa don samun damar ฦ™imar da ke da alaฦ™a da wannan maษ“alli.

Shafi posts:

Leave a Comment