103

I remember there's a redirect function in jQuery.

It was like:

$(location).href('http://address.com')

But what was it exactly? I couldn't remember and can't find it using Google Search.

pwolaq
  • 6,343
  • 19
  • 45
Benjamin
  • 2,073
  • 4
  • 18
  • 16
  • 10
    `$(location).attr('href','http://address.com');` – dexter Aug 16 '11 at 13:08
  • 10
    To clarify other answers, be aware that (a) you don't need jQuery for this; the native approach is perfectly sound; (b) jQuery is just a library of functions; (c) you're still writing _Javascript_. – Lightness Races in Orbit Aug 16 '11 at 14:33
  • $(location) is a selector to instruct Query to refer to the DOM object called location. The object location is an object containing some properties, like href (see https://www.w3schools.com/jsref/obj_location.asp) The property href indicates simply the URL opened in the browser window. Everytime you change it the browser load the new URL. So in the code you indicated you are changing the href property of the DOM object location, using a jQuery style. – Adriano G. V. Esposito Mar 17 '20 at 10:40

6 Answers6

109

There's no need for jQuery.

window.location.href = 'http://example.com';
Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
Dogbert
  • 212,659
  • 41
  • 396
  • 397
32

Use:

window.location.replace(...)

See this Stack Overflow question for more information:

How do I redirect to another webpage?

Or perhaps it was this you remember:

var url = "http://stackoverflow.com";
location.href = url;
Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
Curtis
  • 101,612
  • 66
  • 270
  • 352
29

This is easier:

location.href = 'http://address.com';

Or

location.replace('http://address.com'); // <-- No history saved.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
13

I think you are looking for:

window.location = 'http://someUrl.com';

It's not jQuery; it's pure JavaScript.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
8

Ideally, you want to be using window.location.replace(...).

See this answer here for a full explanation: How do I redirect to another webpage?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
6

You can use just JavaScript:

window.location = 'http://address.com';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robert
  • 8,717
  • 2
  • 27
  • 34