HTML <base> Tag

Introduction to HTML <base> Tag

HTML <base> tag द्वारा किसी HTML document के सभी URLs के लिए base URL specify किया जाता है। इसके बाद आपको पूरा URL लिखने के लिए आवश्यकता नहीं होती है। जब भी आप कोई URL add करते है तो base URL उस URL से पहले automatically prefix कर दिया जाता है।

उदाहरण के लिए मान लीजिये आपकी images web-files directory में है जिसका address c:/documents/web-files है। जब भी आप कोई image add करेंगे तो आपको पूरा address define करना होगा। जैसा की निचे दिया जा रहा है।

<img src="c:/documents/web-files/myImage.png">

इसी प्रकार आप जितनी images add करेंगे उन सबके लिए भी आपको पूरा address specify करना होगा। लेकिन यदि आप web-files directory के address को base url के रूप में define कर दे तो आपको images को add करने के लिए पूरा URL लिखने की आवश्यकता नहीं होगी। जैसा की निचे दिया जा रहा है।

<img src="myImage.png">

Document में specify की गयी सभी links base URL से relative हो जाती है। Base url को page में सबसे ऊपर define किया जा सकता है और बाकी की links को आप short forms में use कर सकते है।

HTML <base> tag को use करके document में code को reduce किया जाता है। इससे code को समझने में आसानी भी होती है।

HTML <base> tag का मुख्य drawback यह है की एक बार इसे define करने के बाद आप bound हो जाते है क्योंकि सभी links के पहले यह automatically prefix हो जाता है। यदि आप किसी दूसरी location से कोई image या file add करना चाहे तो ऐसा करना सम्भव नहीं है।

<base> tag को HTML5 में भी include किया गया है। इसके द्वारा आप सभी URLs के लिए base URL define करते है और इसके अलावा आप सभी links के लिए target भी set कर सकते है।

Syntax of HTML <base> Tag

निचे <base> tag का general syntax दिया जा रहा है।

<base href="base-url" target="_blank">

एक <base> tag में या तो href या target attribute define किया जाना आवश्यक होता है। <base> को बिना attribute के नहीं define किया जा सकता है।

HTML में <base> tag का कोई end tag नहीं होता है। यह एक empty tag है। लेकिन यदि आप इसे XHTML में use कर रहे है तो इसे </base> end tag द्वारा close किया जाना आवश्यक है।

<base> tag को पुरे document में एक ही बार define किया जाना चाहिए। इसे हमेशा <head> tag के अंदर ही define किया जाना चाहिए।

Attributes of HTML <base> Tag

<base> tag के साथ 2 attributes available है। इनके बारे में निचे बताया जा रहा है।

  • href - इस attribute द्वारा base URL specify किया जाता है। URL को double quotes में लिखा जाता है।
  • target - इस attribute द्वारा आप links का target define करते है। इस attribute की निचे दी गयी values define की जा सकती है।
    • _blank - जब आप link को नयी window में open करना चाहते है तो target _blank define करते है।
    • _self - जब आप link को current window में ही open करना चाहते है तो target _self define करते है।
    • _parent - जब आप link को parent container में open करना चाहते है तो target _parent define करते है।

HTML <base> tag global और event attributes को भी support करता है।

Example of HTML <base> Tag

HTML <base> tag का उदाहरण निचे दिया जा रहा है।

<html>
<head>

<title>HTML base tag Demo</title>
<base href="c:/documents/web-files">
</head>

<body>
<img src="myImage.png">
</body>

</html>