URL:

  • JavaScript provides many methods to retrieve and change the current URL which is displayed in browser’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:

[pastacode lang=”java” manual=”var%20currentLocation%20%3D%20window.location%3B%0A” message=”Java Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Basic Structure of a URL:

[pastacode lang=”java” manual=”%09%3Cprotocol%3E%2F%2F%3Chostname%3E%3A%3Cport%3E%2F%3Cpathname%3E%3Csearch%3E%3Chash%3E%0A” message=”Java Code” highlight=”” provider=”manual”/]
  • Protocol – Specifies the protocol name. It is used to access the resource on the Internet. (HTTP (without SSL) or HTTPS (with SSL))
  • hostname – Host name specifies the host that owns the resource. For example, www.wikitechy.com. A server provides services using the name of the host.
  • port – 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.
  • pathname – The path gives info about the specific resource within the host that the Web client needs to access. For example, /index.html.
  • query – 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).
  • hash – The anchor portion of a URL, includes the hash sign (#).

Location object properties can access all of these URL components :

  • hash – It is used to sets or returns the anchor portion of a URL.
    var currenthash = window.location.hash;
  • host – It is used to sets or returns the hostname and port of a URL.
    var currentHost = window.location.host;
  • hostname – It is used to sets or returns the hostname of a URL.
    var currentHostname = window.location.hostname;
  • href – It is used to sets or returns the entire URL.
    var currentURL = window.location.href;
  • pathname – It is used to sets or returns the path name of a URL.
    var currentPathname = window.location.pathname;
  • port –sets or returns the port number the server uses for a URL.
    var currentPort = window.location.port;
  • protocol – It is used to sets or returns the protocol of a URL.
    var currentProtocol = window.location.protocol;
  • search – It is used to sets or returns the query portion of a URL
    var currentSearchString = window.location.search;

Get Current URL in Web browser:

To Get the URL From Firefox and Opera :

[pastacode lang=”java” manual=”DdeClient%20dde%20%3D%20new%20DdeClient(browser%2C%20%22WWW_GetWindowInfo%22)%3B%0Adde.Connect()%3B%0Astring%20url%20%3D%20dde.Request(%22URL%22%2C%20int.MaxValue)%3B%0Astring%5B%5D%20text%20%3D%20url.Split(new%20string%5B%5D%20%7B%20%22%5C%22%2C%5C%22%22%20%7D%2C%20StringSplitOptions.RemoveEmptyEntries)%3B%0Adde.Disconnect()%3B%0Areturn%20text%5B0%5D.Substring(1)%3B%0A%20%0ADownload%20NDde%20dll%20and%20add%20reference%2C%20and%20use%20as%20name%20space%0A%0A%0A%2F%2F%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3DGet%20URL%20of%20All%20Tabs%20From%20IE%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D” message=”” highlight=”” provider=”manual”/] [pastacode lang=”java” manual=”SHDocVw.InternetExplorer%20browser%3B%0Astring%20myLocalLink%3B%0Amshtml.IHTMLDocument2%20myDoc%3B%0ASHDocVw.ShellWindows%20shellWindows%20%3D%20new%20SHDocVw.ShellWindows()%3B%0Astring%20filename%3B%0Aforeach%20(SHDocVw.InternetExplorer%20ie%20in%20shellWindows)%0A%7B%0Afilename%20%3D%20System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower()%3B%0Aif%20((filename%20%3D%3D%20%22iexplore%22))%0A%7B%0Abrowser%20%3D%20ie%3B%0AmyDoc%20%3D%20browser.Document%3B%0AmyLocalLink%20%3D%20myDoc.url%3B%0AMessageBox.Show(myLocalLink)%3B%0A%7D%0A” message=”Java Code” highlight=”” provider=”manual”/]

We can get current page URL in web browser using following code,

[pastacode lang=”java” manual=”Request.Url.AbsoluteUri.ToString()%0A” message=”Java Code” highlight=”” provider=”manual”/]

[pastacode lang=”php” manual=”public%20string%20GetChormeURL(string%20ProcessName)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20string%20ret%20%3D%20%22%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Process%5B%5D%20procs%20%3D%20Process.GetProcessesByName(ProcessName)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20foreach%20(Process%20proc%20in%20procs)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20the%20chrome%20process%20must%20have%20a%20window%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(proc.MainWindowHandle%20%3D%3D%20IntPtr.Zero)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2FAutomationElement%20elm%20%3D%20AutomationElement.RootElement.FindFirst(TreeScope.Children%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20new%20PropertyCondition(AutomationElement.ClassNameProperty%2C%20%22Chrome_WidgetWin_1%22))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20find%20the%20automation%20element%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20AutomationElement%20elm%20%3D%20AutomationElement.FromHandle(proc.MainWindowHandle)%3B%0A%0A” message=”Java Code” highlight=”” provider=”manual”/] [pastacode lang=”java” manual=”%20%2F%2F%20manually%20walk%20through%20the%20tree%2C%20searching%20using%20TreeScope.Descendants%20is%20too%20slow%20(even%20if%20it’s%20more%20reliable)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20AutomationElement%20elmUrlBar%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20walking%20path%20found%20using%20inspect.exe%20(Windows%20SDK)%20for%20Chrome%2043.0.2357.81%20m%20(currently%20the%20latest%20stable)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Inspect.exe%20path%20-%20C%3A%2F%2FProgram%20files%20(X86)%2FWindows%20Kits%2F10%2Fbin%2Fx64%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20elm1%20%3D%20elm.FindFirst(TreeScope.Children%2C%20new%20PropertyCondition(AutomationElement.NameProperty%2C%20%22Google%20Chrome%22))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(elm1%20%3D%3D%20null)%20%7B%20continue%3B%20%7D%20%2F%2F%20not%20the%20right%20chrome.exe%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20elm2%20%3D%20TreeWalker.RawViewWalker.GetLastChild(elm1)%3B%20%2F%2F%20I%20don’t%20know%20a%20Condition%20for%20this%20for%20finding%0A” message=”Java Code” highlight=”” provider=”manual”/] [pastacode lang=”java” manual=”%20var%20elm3%20%3D%20elm2.FindFirst(TreeScope.Children%2C%20new%20PropertyCondition(AutomationElement.NameProperty%2C%20%22%22))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20elm4%20%3D%20TreeWalker.RawViewWalker.GetNextSibling(elm3)%3B%20%2F%2F%20I%20don’t%20know%20a%20Condition%20for%20this%20for%20finding%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20elm5%20%3D%20elm4.FindFirst(TreeScope.Children%2C%20new%20PropertyCondition(AutomationElement.ControlTypeProperty%2C%20ControlType.ToolBar))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20elm6%20%3D%20elm5.FindFirst(TreeScope.Children%2C%20new%20PropertyCondition(AutomationElement.NameProperty%2C%20%22%22))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elmUrlBar%20%3D%20elm6.FindFirst(TreeScope.Children%2C%20new%20PropertyCondition(AutomationElement.ControlTypeProperty%2C%20ControlType.Edit))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20catch%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Chrome%20has%20probably%20changed%20something%2C%20and%20above%20walking%20needs%20to%20be%20modified.%20%3A(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20put%20an%20assertion%20here%20or%20something%20to%20make%20sure%20you%20don’t%20miss%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%0A” message=”Java Code” highlight=”” provider=”manual”/] [ad type=”banner”] [pastacode lang=”java” manual=”%20%2F%2F%20make%20sure%20it’s%20valid%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(elmUrlBar%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20it’s%20not..%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20elmUrlBar%20is%20now%20the%20URL%20bar%20element.%20we%20have%20to%20make%20sure%20that%20it’s%20out%20of%20keyboard%20focus%20if%20we%20want%20to%20get%20a%20valid%20URL%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20there%20might%20not%20be%20a%20valid%20pattern%20to%20use%2C%20so%20we%20have%20to%20make%20sure%20we%20have%20one%0A” message=”Java Code” highlight=”” provider=”manual”/] [pastacode lang=”javascript” manual=”%20AutomationPattern%5B%5D%20patterns%20%3D%20elmUrlBar.GetSupportedPatterns()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(patterns.Length%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ret%20%3D%20((ValuePattern)elmUrlBar.GetCurrentPattern(patterns%5B0%5D)).Current.Value%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20ret%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20catch%20%7B%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(ret%20!%3D%20%22%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20must%20match%20a%20domain%20name%20(and%20possibly%20%22https%3A%2F%2F%22%20in%20front)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(Regex.IsMatch(ret%2C%20%40%22%5E(https%3A%5C%2F%5C%2F)%3F%5Ba-zA-Z0-9%5C-%5C.%5D%2B(%5C.%5Ba-zA-Z%5D%7B2%2C4%7D).*%24%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20prepend%20http%3A%2F%2F%20to%20the%20url%2C%20because%20Chrome%20hides%20it%20if%20it’s%20not%20SSL%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(!ret.StartsWith(%22http%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ret%20%3D%20%22http%3A%2F%2F%22%20%2B%20ret%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A” message=”java Code” highlight=”” provider=”manual”/] [pastacode lang=”java” manual=”%20return%20ret%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20ret%3B%0A%20%20%20%20%20%20%20%20%7D%0A” message=”Java Code” highlight=”” provider=”manual”/]

To get the path, we can use the method:

[pastacode lang=”java” manual=”alert(%22Url%20%20%3D%22%2Bdocument.location)%3B%0Aalert(%22PathName%20%20%3D%22%2B%20window.location.pathname)%3B%2F%2F%20Returns%20path%20only%0Aalert(%22url%20%20%3D%22%2Bwindow.location.href)%3B%2F%2F%20Returns%20full%20URL%0A” message=”Java Code” highlight=”” provider=”manual”/]

Use window.location.href to get the complete URL.
Use window.location.pathname to get URL leaving the host.

For complete URL with query strings:

[pastacode lang=”java” manual=”document.location.toString().toLowerCase()%3B” message=”Java Code” highlight=”” provider=”manual”/] [ad type=”banner”]

For host URL:

[pastacode lang=”java” manual=”window.location” message=”Java Code” highlight=”” provider=”manual”/]

Categorized in: