• Skip to main content
  • Select language
  • Skip to search

Location.assign()

The Location .assign()  method causes the window to load and display the document at the URL specified.

If the assignment can't happen because of a security violation, a DOMException of the SECURITY_ERROR type is thrown. This happens if the origin of the script calling the method is different from the origin of the page originally described by the Location object, mostly when the script is hosted on a different domain.

If the provided URL is not valid, a DOMException of the SYNTAX_ERROR type is thrown.

Specifications

Browser compatibility.

  • The Location interface it belongs to.
  • Similar methods: Location.replace() and Location.reload() .

Document Tags and Contributors

  • autocomplete
  • autocompleteerror
  • DOMContentLoaded
  • afterscriptexecute
  • beforeprint
  • beforescriptexecute
  • beforeunload
  • contextmenu
  • languagechange
  • readystatechange
  • loadedmetadata
  • canplaythrough
  • volumechange
  • durationchange
  • unhandledrejection
  • rejectionhandled
  • BeforeUnloadEvent
  • DOMStringMap
  • GlobalEventHandlers
  • HTMLAnchorElement
  • HTMLAreaElement
  • HTMLAudioElement
  • HTMLBRElement
  • HTMLBaseElement
  • HTMLBaseFontElement
  • HTMLBodyElement
  • HTMLButtonElement
  • HTMLCanvasElement
  • HTMLContentElement
  • HTMLDListElement
  • HTMLDataElement
  • HTMLDataListElement
  • HTMLDialogElement
  • HTMLDivElement
  • HTMLDocument
  • HTMLElement
  • HTMLEmbedElement
  • HTMLFieldSetElement
  • HTMLFormControlsCollection
  • HTMLFormElement
  • HTMLFrameSetElement
  • HTMLHRElement
  • HTMLHeadElement
  • HTMLHeadingElement
  • HTMLHtmlElement
  • HTMLIFrameElement
  • HTMLImageElement
  • HTMLInputElement
  • HTMLIsIndexElement
  • HTMLKeygenElement
  • HTMLLIElement
  • HTMLLabelElement
  • HTMLLegendElement
  • HTMLLinkElement
  • HTMLMapElement
  • HTMLMediaElement
  • HTMLMetaElement
  • HTMLMeterElement
  • HTMLModElement
  • HTMLOListElement
  • HTMLObjectElement
  • HTMLOptGroupElement
  • HTMLOptionElement
  • HTMLOptionsCollection
  • HTMLOutputElement
  • HTMLParagraphElement
  • HTMLParamElement
  • HTMLPictureElement
  • HTMLPreElement
  • HTMLProgressElement
  • HTMLQuoteElement
  • HTMLScriptElement
  • HTMLSelectElement
  • HTMLShadowElement
  • HTMLSourceElement
  • HTMLSpanElement
  • HTMLStyleElement
  • HTMLTableCaptionElement
  • HTMLTableCellElement
  • HTMLTableColElement
  • HTMLTableDataCellElement
  • HTMLTableElement
  • HTMLTableHeaderCellElement
  • HTMLTableRowElement
  • HTMLTableSectionElement
  • HTMLTemplateElement
  • HTMLTextAreaElement
  • HTMLTimeElement
  • HTMLTitleElement
  • HTMLTrackElement
  • HTMLUListElement
  • HTMLUnknownElement
  • HTMLVideoElement
  • HashChangeEvent
  • MessageChannel
  • MessageEvent
  • MessagePort
  • NavigatorGeolocation
  • NavigatorID
  • NavigatorLanguage
  • NavigatorOnLine
  • NavigatorPlugins
  • PageTransitionEvent
  • PluginArray
  • PopStateEvent
  • PortCollection
  • PromiseRejectionEvent
  • RadioNodeList
  • Transferable
  • ValidityState
  • WindowBase64
  • WindowEventHandlers
  • WindowTimers

JS Tutorial

Js versions, js functions, js html dom, js browser bom, js web apis, js vs jquery, js graphics, js examples, js references, javascript window location.

The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.

Window Location

The window.location object can be written without the window prefix.

Some examples:

  • window.location.href returns the href (URL) of the current page
  • window.location.hostname returns the domain name of the web host
  • window.location.pathname returns the path and filename of the current page
  • window.location.protocol returns the web protocol used (http: or https:)
  • window.location.assign() loads a new document

Window Location Href

The window.location.href property returns the URL of the current page.

Display the href (URL) of the current page:

Window Location Hostname

The window.location.hostname property returns the name of the internet host (of the current page).

Display the name of the host:

Advertisement

Window Location Pathname

The window.location.pathname property returns the pathname of the current page.

Display the path name of the current URL:

Window Location Protocol

The window.location.protocol property returns the web protocol of the page.

Display the web protocol:

Window Location Port

The window.location.port property returns the number of the internet host port (of the current page).

Most browsers will not display default port numbers (80 for http and 443 for https)

Window Location Assign

The window.location.assign() method loads a new document.

Load a new document:

Get Certified

COLOR PICKER

colorpicker

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

COMMENTS

  1. javascript

    Using window.location.assign("url") will just cause a new document to load. Using window.location.replace("url") will replace the current document and replace the current History with that URL making it so you can't go back to the previous document loaded.

  2. Location assign() Method

    Note. The difference between assign () and replace (): replace () removes the current URL from the document history. With replace () it is not possible to use "back" to navigate back to the original document.

  3. Window: location property

    Window: location property. The Window.location read-only property returns a Location object with information about the current location of the document. Though Window.location is a read-only Location object, you can also assign a string to it. This means that you can work with location as if it were a string in most cases: location = 'http ...

  4. Location.assign()

    Location. .assign () The Location .assign () method causes the window to load and display the document at the URL specified. If the assignment can't happen because of a security violation, a DOMException of the SECURITY_ERROR type is thrown. This happens if the origin of the script calling the method is different from the origin of the page ...

  5. JavaScript Window Location

    The window.location object can be written without the window prefix. Some examples: window.location.href returns the href (URL) of the current page. window.location.hostname returns the domain name of the web host. window.location.pathname returns the path and filename of the current page. window.location.protocol returns the web protocol used ...