जावास्क्रिप्ट मध्ये जी लिंक म्हणजेच URL तुमच्या ऍड्रेस बार मध्ये दिसत आहे ती कशी मिळवायची हे आपण या आर्टिकल मध्ये बघणार आहोत.
तर मुळात याचा कोड फारच सोपा आहे. अगदीच २ ओळींचा हा कोड आहे.
function getCurrentURL () { return window.location.href } const url = getCurrentURL()
फक्त एवढाच कोड जर तुम्ही रन केलात तर तुम्हाला ब्राउजर मध्ये असलेला URL तुमच्या अप्लिकेशन मध्ये वापरता येईल.
ES६ मध्ये तुम्हाला माहित आहे कि ऑब्जेक्ट तुम्हाला डीस्ट्रक्चर करता येतो. तर जो window.location.href हा ऑब्जेक्ट आहे याच्या प्रॉपर्टी पण आपण डीस्ट्रक्चर करू शकतो.
const { host, hostname, href, origin, pathname, port, protocol, search } = window.location
मग जर आपण आता ज्या url वर आहे त्याचा अभ्यास केला तर वरील गोष्टी काय काय येतील?
https://clickskills.in/mr/blog/posts/get-current-url-in-javascript-in-marathi //host: clickskills.in //hostname: clickskills //href: https://clickskills.in/mr/blog/posts/get-current-url-in-javascript-in-marathi //origin: https://clickskills.in //pathname: /mr/blog/posts/get-current-url-in-javascript-in-marathi //port: "" //protocol: https: //search: ""
तर अशा पद्धतीने आपण जावास्क्रिप्ट मधील URL चे वेगवेगळे भाग वापरू शकतो.



Comments
No comments yet
Be the first to share your thoughts on this article