What happens to code after a javascript redirect (setting window.location.href)?

Asked
Active3 hr before
Viewed126 times

7 Answers

javascript
90%

Meta Stack Overflow , Stack Overflow help chat ,Stack Overflow en español,Stack Overflow на русском

The browser will try to execute the code after window.location.href = 'url' until the page goes to the next web adress, so the number of lines of code that will be executed depends on the browser's speed

window.location.href = 'url'
load more v
88%

I have the following javascript redirect code followed by some more code.,In firefox, the first alert is visible for a split second right before getting redirected. The second alert is not visible at all.,The Javascript session will struggle to continue it's mission in the face of impending doom. Coding in this manner is considered unpredictable. ...and cruel.,In chrome, both alerts will pop up, and the redirect happens only after clicking ok for both alerts.

I have the following javascript redirect code followed by some more code.

window.location.href = '/someurl';
alert('hello');
alert('hello again');
72%

I have the following javascript redirect code followed by some more code.,In firefox, the first alert is visible for a split second right before getting redirected. The second alert is not visible at all.,However, there is another hitch, since that script is using window.onload. Chrome userscripts, with the default execution start-mode, will often never see the onload event.,So the script becomes:

I have the following javascript redirect code followed by some more code.

window.location.href = '/someurl';
alert('hello');
alert('hello again');
load more v
65%

In order to redirect the user to another website immediately after your website is opened, you can use the following code at the top of your page, within the <head> element. Or, if you are using a separate .js file, put the following code into that file and remember to link to it from the head of your page.,In order to redirect the user to another website after a certain time passes, you can use the following code:,Thank you for everyone who joined us this past year to hear about our proven methods of attracting and retaining tech talent.,Simply insert your target URL that you want to redirect to in the above code. You can also check this page to read more about how window.location works. Now, let’s continue with our examples.

In JavaScript, window.location or simply location object is used to get information about the location of the current web page (document) and also to modify it. The following is a list of possible ways that can be used as a JavaScript redirect:

// Sets the new location of the current window.
window.location = "https://www.example.com";

// Sets the new href (URL) for the current window.
window.location.href = "https://www.example.com";

// Assigns a new URL to the current window.
window.location.assign("https://www.example.com");

// Replaces the location of the current window with the new one.
window.location.replace("https://www.example.com");

// Sets the location of the current window itself.
self.location = "https://www.example.com";

// Sets the location of the topmost window of the current window.
top.location = "https://www.example.com";
load more v
75%

Example 1: This example points to the same URL and only redirects to the same address.URL = ‘https://gitstr.herokuapp.com’,After clicking on the Search button:as you can see after clicking the search button the URL doesn’t change because of line 38: window.location.href = ‘/’.,Example 2: In this example we would like to use window.location.href propertyto point to some other address, so let’s see how it works.,How to redirect to another webpage in HTML?

40%

//If URL contains "mystring"
if (window.location.href.indexOf("mystring") > -1) {

}
load more v
22%

How to test chenge location in vuex action ?,"In your Jest configuration, make sure to set the following:,old ticket but for those still having this issue we've started using window.location.assign() instead so in our tests we can mock the assign function like so..,A click event that is changing the window.location can now be captured.

jest.autoMockOff()
jest.setMock('../lib/window', window)

jest.mock('cookies-js')
jest.mock('query-string')
jest.mock('superagent')

describe(['@utils/auth - When an AJAX response returns:'].join(''), function() {

         beforeEach(function() {
            window.location.href = 'http://quux.default.com'
            var queryString = require('query-string')
            queryString.__setMockParseReturns([{
               'access_token': '1234foo',
               'expires_in': '9999'
            }])
         })

         it(['should set a redirect token and goto platform ',
            'when the AJAX request returns 401.'
         ].join(''), function() {
            var superagent = require('superagent')
            superagent.__setMockAjaxResponses([
               [null, {
                  'status': 401
               }]
            ])

            var href = window.location.href
            var auth = require('../index.js')
            auth.login(function(res) {})
            var Cookies = require('cookies-js')
            var config = require.requireActual('../config')
            expect(decodeURIComponent(window.location.href)).toBe([
               config.loginUrl,
               config.loginServiceLogin,
               '?client_id=',
               config.clientId,
               '&response_type=token',
               '&redirect_uri=',
               config.clientRedirectUri
            ].join(''))
            expect(Cookies.__getMockCookieData()[config.clientId + '_state_locationAfterLogin']).toBe(escape(href))
         })
load more v

Other "javascript-undefined" queries related to "What happens to code after a javascript redirect (setting window.location.href)?"