This blog all about how to get the base URL using jQuery. For example, if the total URL is http://192.168.1.147:8080/sign/
The output will be http://192.168.1.147:8080 . Here base URL can be obtained by simply using jquery attr() in-built method by passing the parameter “origin”, Find the below code to get the base URL for a page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>How to Get Base URL Using jQuery</title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".getBUrl").click(function(){ var getBUrl = $(location).attr("origin"); alert(getBUrl); }); }); </script> <style type="text/css"> .container{ margin-left: 350px; margin-top: 100px; } </style> </head> <body> <div class="container"> <h1>How to Get Base URL Using jQuery</h1> <div style="margin-left: 200px;"><button type="button" class="getBUrl">Get Base URL</button></div> </div> </body> </html> |