How to get the browser to navigate to URL in JavaScript
This works in all browsers:
window.location.href = '...';
If you wanted to change the page without it reflecting in the browser back history, you can do:
window.location.replace('...');
Javascript method to navigate to other url
You can use
window.location="http://www.dignaj.com";
You can also use navigate like this (only works in some IE)
window.navigate("http://www.dignaj.com");
Get the current URL with JavaScript?
Use:
window.location.href
As noted in the comments, the line below works, but it is bugged for Firefox.
document.URL
See URL of type DOMString, readonly.
How do I redirect to another webpage?
One does not simply redirect using jQuery
jQuery is not necessary, and window.location.replace(...)
will best simulate an HTTP redirect.
window.location.replace(...)
is better than using window.location.href
, because replace()
does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.
If you want to simulate someone clicking on a link, use
location.href
If you want to simulate an HTTP redirect, use location.replace
For example:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
Web Browser Control - Navigate to URL one after the other
I managed to solve this issue myself and thought I'd post the answer for someone else who might be trying to resolve a similar issue.
Basically, what I did was, instead of looping through the list of URL's one by one, I waited for the documentcomplete event to finish and once it does that, I use the same webbrowserform and perform another navigate until all my URL's have been completely navigated, after which I close the form.
Code:
Public currentUrlIndex As Integer = Nothing, currentUrl As String = Nothing
Public Sub customNavigation()
testWebBrowserForm = New WebBrowserForm(Me)
Dim browserSize As System.Drawing.Size = New Size(100, 100)
testWebBrowserForm.Size = browserSize
testWebBrowserForm.FormBorderStyle = FormBorderStyle.FixedSingle
testWebBrowserForm.Show()
testWebBrowserForm.SendToBack()
testWebBrowserForm.Location = New Point(100, 100)
currentUrlIndex = 0
currentUrl = listOfUrls(currentUrlIndex)
testWebBrowserForm.Navigate(New Uri(currentUrl))
End Sub
// Once the document has completely loaded
Public Sub documentLoadComplete()
Dim submitButton As HtmlElement = Nothing, formEl As HtmlElement = Nothing
Dim attachmentInputElements As Windows.Forms.HtmlElementCollection = testWebBrowserForm.webBrowser.Document.GetElementsByTagName("input")
Dim formElements As Windows.Forms.HtmlElementCollection = testWebBrowserForm.webBrowser.Document.Forms
Dim form As Windows.Forms.HtmlElement = testWebBrowserForm.webBrowser.Document.Forms(0)
For y = 0 To formElements.Count - 1
Dim formelement As HtmlElement = formElements(y)
If formelement.GetAttribute("name").Equals("theForm") Then
formEl = formelement
End If
Next
For i = 0 To attachmentInputElements.Count - 1
Dim inputElement As HtmlElement = attachmentInputElements(i)
If inputElement.GetAttribute("type").Equals("submit") Then
submitButton = inputElement
End If
Next
testWebBrowserForm.webBrowser.Document.InvokeScript("doSomething")
submitButton.InvokeMember("click")
If currentUrlIndex = listOfUrls.Count - 1 Then
testWebBrowserForm.Close()
Exit Sub
Else
currentUrlIndex = currentUrlIndex + 1
currentUrl = listOfUrls(currentUrlIndex)
testWebBrowserForm.Navigate(New Uri(currentUrl))
End If
End Sub
How to get the previous URL in JavaScript?
document.referrer
in many cases will get you the URL of the last page the user visited, if they got to the current page by clicking a link (versus typing directly into the address bar, or I believe in some cases, by submitting a form?). Specified by DOM Level 2. More here.
window.history
allows navigation, but not access to URLs in the session for security and privacy reasons. If more detailed URL history was available, then every site you visit could see all the other sites you'd been to.
If you're dealing with state moving around your own site, then it's possibly less fragile and certainly more useful to use one of the normal session management techniques: cookie data, URL params, or server side session info.
How to navigate url using pushState
The entire point of pushState
is that it doesn't trigger navigation. It is a mechanism for saying "I am changing the state of the DOM using JavaScript, the resulting state is the same as you would get if you visited this URL" (while making the back and forward navigation buttons built into the browser work).
If you want to navigate with JavaScript then assign a new value to location.href
.
location.href = "/upload/"
However, since your JavaScript doesn't do anything except navigate, you should just be using a regular link for this in the first place:
<a href="/upload/">Upload Page</a>
Javascript Get Website URL
There are several ways you can do this, but one way might be best for certain situations (e.g. within an iFrame).
Protocol + Domain + Page
document.URL
> "http://example.com/page1.html"
document.location.href
> "http://example.com/page1.html"
Protocol + Domain
document.location.origin
> "http://example.com"
Domain
document.location.host
> "example.com"
Page
document.location.pathname
> "/page1.html"
Related Topics
JavaScript Incapable of Getting Element's Max-Height via Element.Style.Maxheight
Changed Id Not Being Found by Jquery
Is There an Equivalent of the _Nosuchmethod_ Feature for Properties, or a Way to Implement It in Js
How to Open an External Link in Safari Not the App's Uiwebview
Js & Jquery Can't Detect HTML Elements, and Say's They Are Undefined
How to Convert a Float Number to a Whole Number in JavaScript
Declaring JavaScript Object Method in Constructor Function VS. in Prototype
How to Display All Methods of an Object
Escaping </Script> Tag Inside JavaScript
How to Ignore JavaScript Exceptions When Working with Webdriver (Htmlunit, Ruby Bindings)
Call Swift Function with JavaScript Using Uiwebview
Rendering Raw HTML with Reactjs
Does Every JavaScript Function Have to Return a Value
How to Break Line in JavaScript
How to Make a Radio Button Unchecked by Clicking It