{"id":1706,"date":"2017-03-22T18:35:01","date_gmt":"2017-03-22T13:05:01","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1706"},"modified":"2018-10-25T18:07:28","modified_gmt":"2018-10-25T12:37:28","slug":"get-current-url-web-browser","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/get-current-url-web-browser\/","title":{"rendered":"Get current URL in web browser &#8211; JAVA"},"content":{"rendered":"<h2 id=\"url\"><span style=\"color: #800000;\"><strong>URL:<\/strong><\/span><\/h2>\n<ul>\n<li><a href=\"https:\/\/www.wikitechy.com\/technology\/get-size-screen-current-web-page-browser-window\/\" target=\"_blank\" rel=\"noopener\">JavaScript<\/a> provides many methods to retrieve and <strong>change the current URL<\/strong> which is displayed in browser&#8217;s address bar.<\/li>\n<li>All these methods uses the <strong>Location object<\/strong>, which is a property of the Window object.<\/li>\n<\/ul>\n<h4 id=\"we-can-create-a-new-location-object-that-has-the-current-url-as-follows\"><span style=\"color: #ff6600;\"><strong>we can create a new Location object that has the current URL as follows:<\/strong><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">var currentLocation = window.location;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h3 id=\"basic-structure-of-a-url\"><span style=\"color: #3366ff;\">Basic Structure of a URL:<\/span><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\t&lt;protocol&gt;\/\/&lt;hostname&gt;:&lt;port&gt;\/&lt;pathname&gt;&lt;search&gt;&lt;hash&gt;<\/code><\/pre> <\/div>\n<ul>\n<li><span style=\"color: #800080;\"><strong>Protocol<\/strong><\/span> \u2013 Specifies the protocol name. It is used to access the resource on the Internet. (HTTP (without SSL) or HTTPS (with SSL))<\/li>\n<li><span style=\"color: #800080;\"><strong>hostname<\/strong><\/span>\u00a0&#8211; Host name specifies the host that owns the resource. For example, <a href=\"https:\/\/www.wikitechy.com\" target=\"_blank\" rel=\"noopener\">www.wikitechy.com<\/a>. A server provides services using the name of the host.<\/li>\n<li><span style=\"color: #800080;\"><strong>port<\/strong> <\/span>&#8211; A port number used to recognize a specific process to which an Internet or other network message is to be forwarded when it arrives at a server.<\/li>\n<li><span style=\"color: #800080;\"><strong>pathname<\/strong><\/span>\u00a0&#8211; The path gives info about the specific resource within the host that the Web client needs to access. For example, \/index.html.<\/li>\n<li><span style=\"color: #800080;\"><strong>query<\/strong><\/span> &#8211; A query string follows the path component, and provides a string of information that the resource can utilize for some purpose (for example, as parameters for a search or as data to be processed).<\/li>\n<li><span style=\"color: #800080;\"><strong>hash<\/strong><\/span> &#8211; The anchor portion of a URL, includes the hash sign (#).<\/li>\n<\/ul>\n<h3 id=\"location-object-properties-can-access-all-of-these-url-components\"><span style=\"color: #ff0000;\"><strong>Location object properties can access all of these URL components :<\/strong><\/span><\/h3>\n<ul>\n<li><span style=\"color: #3366ff;\"><strong>hash<\/strong><\/span> \u2013 It is used to sets or returns the anchor portion of a URL.<br \/>\nvar currenthash = window.location.hash;<\/li>\n<li><span style=\"color: #3366ff;\"><strong>host<\/strong> <\/span>\u2013 It is used to sets or returns the hostname and port of a URL.<br \/>\nvar currentHost = window.location.host;<\/li>\n<li><span style=\"color: #3366ff;\"><strong>hostname<\/strong><\/span> \u2013 It is used to sets or returns the hostname of a URL.<br \/>\nvar currentHostname = window.location.hostname;<\/li>\n<li><span style=\"color: #3366ff;\"><strong>href<\/strong> <\/span>\u2013 It is used to sets or returns the entire URL.<br \/>\nvar currentURL = window.location.href;<\/li>\n<li><span style=\"color: #3366ff;\"><strong>pathname<\/strong><\/span> \u2013 It is used to sets or returns the path name of a URL.<br \/>\nvar currentPathname = window.location.pathname;<\/li>\n<li><span style=\"color: #3366ff;\"><strong>port<\/strong> <\/span>\u2013sets or returns the port number the <a href=\"https:\/\/www.wikitechy.com\/final-year-project\/dotnet\/server-hacking\/what-are-the-types-of-web-server\" target=\"_blank\" rel=\"noopener\">server<\/a> uses for a URL.<br \/>\nvar currentPort = window.location.port;<\/li>\n<li><span style=\"color: #3366ff;\"><strong>protocol<\/strong> <\/span>\u2013 It is used to sets or returns the protocol of a URL.<br \/>\nvar currentProtocol = window.location.protocol;<\/li>\n<li><span style=\"color: #3366ff;\"><strong>search<\/strong><\/span> \u2013 It is used to sets or returns the query portion of a URL<br \/>\nvar currentSearchString = window.location.search;<\/li>\n<\/ul>\n<h2 id=\"get-current-url-in-web-browser\"><span style=\"color: #003366;\">Get Current URL in Web browser:<\/span><\/h2>\n<h3 id=\"solution-1\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 1:<\/label><\/span><\/h3>\n<h3 id=\"to-get-the-url-from-firefox-and-opera\"><span style=\"color: #339966;\">To Get the URL From Firefox and Opera :<\/span><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">DdeClient dde = new DdeClient(browser, &quot;WWW_GetWindowInfo&quot;);<br\/>dde.Connect();<br\/>string url = dde.Request(&quot;URL&quot;, int.MaxValue);<br\/>string[] text = url.Split(new string[] { &quot;\\&quot;,\\&quot;&quot; }, StringSplitOptions.RemoveEmptyEntries);<br\/>dde.Disconnect();<br\/>return text[0].Substring(1);<br\/> <br\/>Download NDde dll and add reference, and use as name space<br\/><br\/><br\/>\/\/===========Get URL of All Tabs From IE ======================<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">SHDocVw.InternetExplorer browser;<br\/>string myLocalLink;<br\/>mshtml.IHTMLDocument2 myDoc;<br\/>SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();<br\/>string filename;<br\/>foreach (SHDocVw.InternetExplorer ie in shellWindows)<br\/>{<br\/>filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower();<br\/>if ((filename == &quot;iexplore&quot;))<br\/>{<br\/>browser = ie;<br\/>myDoc = browser.Document;<br\/>myLocalLink = myDoc.url;<br\/>MessageBox.Show(myLocalLink);<br\/>}<\/code><\/pre> <\/div>\n<h3 id=\"solution-2\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 2:<\/label><\/span><\/h3>\n<p>We can get current page URL in web browser\u00a0using following code,<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">Request.Url.AbsoluteUri.ToString()<\/code><\/pre> <\/div>\n<h3 id=\"solution-3\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 3:<\/label><\/span><\/h3>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">public string GetChormeURL(string ProcessName)<br\/>        {<br\/>            string ret = &quot;&quot;;<br\/>            Process[] procs = Process.GetProcessesByName(ProcessName);<br\/>            foreach (Process proc in procs)<br\/>            {<br\/>                \/\/ the chrome process must have a window<br\/>                if (proc.MainWindowHandle == IntPtr.Zero)<br\/>                {<br\/>                    continue;<br\/>                }<br\/>                \/\/AutomationElement elm = AutomationElement.RootElement.FindFirst(TreeScope.Children,<br\/>                \/\/         new PropertyCondition(AutomationElement.ClassNameProperty, &quot;Chrome_WidgetWin_1&quot;));<br\/>                \/\/ find the automation element<br\/>                AutomationElement elm = AutomationElement.FromHandle(proc.MainWindowHandle);<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\"> \/\/ manually walk through the tree, searching using TreeScope.Descendants is too slow (even if it&#039;s more reliable)<br\/>                AutomationElement elmUrlBar = null;<br\/>                try<br\/>                {<br\/>                    \/\/ walking path found using inspect.exe (Windows SDK) for Chrome 43.0.2357.81 m (currently the latest stable)<br\/>                    \/\/ Inspect.exe path - C:\/\/Program files (X86)\/Windows Kits\/10\/bin\/x64<br\/>                    var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, &quot;Google Chrome&quot;));<br\/>                    if (elm1 == null) { continue; } \/\/ not the right chrome.exe<br\/>                    var elm2 = TreeWalker.RawViewWalker.GetLastChild(elm1); \/\/ I don&#039;t know a Condition for this for finding<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\"> var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, &quot;&quot;));<br\/>                    var elm4 = TreeWalker.RawViewWalker.GetNextSibling(elm3); \/\/ I don&#039;t know a Condition for this for finding<br\/>                    var elm5 = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));<br\/>                    var elm6 = elm5.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, &quot;&quot;));<br\/>                    elmUrlBar = elm6.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));<br\/>                }<br\/>                catch<br\/>                {<br\/>                    \/\/ Chrome has probably changed something, and above walking needs to be modified. :(<br\/>                    \/\/ put an assertion here or something to make sure you don&#039;t miss it<br\/>                    continue;<br\/>                }<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\"> \/\/ make sure it&#039;s valid<br\/>                if (elmUrlBar == null)<br\/>                {<br\/>                    \/\/ it&#039;s not..<br\/>                    continue;<br\/>                }<br\/><br\/>                \/\/ elmUrlBar is now the URL bar element. we have to make sure that it&#039;s out of keyboard focus if we want to get a valid URL<br\/>                if ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))<br\/>                {<br\/>                    continue;<br\/>                }<br\/><br\/>                \/\/ there might not be a valid pattern to use, so we have to make sure we have one<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\"> AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();<br\/>                if (patterns.Length == 1)<br\/>                {<br\/>                    try<br\/>                    {<br\/>                        ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;<br\/>                        return ret;<br\/>                    }<br\/> catch { }<br\/>                    if (ret != &quot;&quot;)<br\/>                    {<br\/>                        \/\/ must match a domain name (and possibly &quot;https:\/\/&quot; in front)<br\/>                        if (Regex.IsMatch(ret, @&quot;^(https:\\\/\\\/)?[a-zA-Z0-9\\-\\.]+(\\.[a-zA-Z]{2,4}).*$&quot;))<br\/>                        {<br\/>                            \/\/ prepend http:\/\/ to the url, because Chrome hides it if it&#039;s not SSL<br\/>                            if (!ret.StartsWith(&quot;http&quot;))<br\/>                            {<br\/>                                ret = &quot;http:\/\/&quot; + ret;<br\/>                            }<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\"> return ret;<br\/>                        }<br\/>                    }<br\/>                    continue;<br\/>                }<br\/>            }<br\/>            return ret;<br\/>        }<\/code><\/pre> <\/div>\n<h3 id=\"solution-4\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 4:<\/label><\/span><\/h3>\n<p>To get the path, we can use the method:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">alert(&quot;Url  =&quot;+document.location);<br\/>alert(&quot;PathName  =&quot;+ window.location.pathname);\/\/ Returns path only<br\/>alert(&quot;url  =&quot;+window.location.href);\/\/ Returns full URL<\/code><\/pre> <\/div>\n<h3 id=\"solution-5\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 5:<\/label><\/span><\/h3>\n<p>Use<strong> window.location.href<\/strong> to get the complete URL.<br \/>\nUse<strong> window.location.pathname<\/strong> to get URL leaving the host.<\/p>\n<h3 id=\"solution-6\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 6:<\/label><\/span><\/h3>\n<p>For <strong>complete URL with query strings<\/strong>:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">document.location.toString().toLowerCase();<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"for-host-url\"><strong>For host URL:<\/strong><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">window.location<\/code><\/pre> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>URL: JavaScript provides many methods to retrieve and change the current URL which is displayed in browser&#8217;s address bar. All these methods uses the Location object, which is a property of the Window object. we can create a new Location object that has the current URL as follows: [ad type=&#8221;banner&#8221;] Basic Structure of a URL: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139],"tags":[3679,3673,3680,1630,3688,3682,3686,3675,3681,3676,3678,3674,3687,3684,3685,3683,3672,3677],"class_list":["post-1706","post","type-post","status-publish","format-standard","hentry","category-java","tag-solved-c-webbrowserhow-can-i-get-the-url-of-hyperlink-i-clicked","tag-c-how-to-get-the-url-of-a-webbrowser-control","tag-display-current-webbrowser-url-in-textbox","tag-get-current-page-url-javascript","tag-get-current-url-c","tag-get-current-url-jquery","tag-get-current-url-php","tag-get-url-from-webbrowser-control","tag-how-can-i-get-the-current-full-url-displayed-in-the-browser","tag-how-to-get-current-url-in-web-browser-using-javascript","tag-how-to-get-url-form-any-web-browser","tag-javascript-get-current-url-in-web-browser","tag-javascript-get-base-url","tag-javascript-get-current-url-path","tag-javascript-get-url-parameter","tag-javascript-get-url-path","tag-related-queries-get-the-current-url-of-web-browser-in-c","tag-vs-2008-getting-current-url-of-webbrowser-and-converting-to-string"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1706"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1706\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}