100

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,123
  • 18
  • 45
Benjamin
  • 1,953
  • 4
  • 17
  • 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

106

There's no need for jQuery.

window.location.href = 'http://example.com';
Timo Tijhof
  • 9,867
  • 6
  • 33
  • 47
Dogbert
  • 200,802
  • 40
  • 378
  • 386
33

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
  • 9,867
  • 6
  • 33
  • 47
Curtis
  • 98,395
  • 62
  • 265
  • 345
29

This is easier:

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

Or

location.replace('http://address.com'); // <-- No history saved.
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Joseph Marikle
  • 72,900
  • 16
  • 109
  • 126
13

I think you are looking for:

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

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
totallyNotLizards
  • 8,209
  • 9
  • 48
  • 84
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,030
  • 21
  • 100
  • 124
Jamie Dixon
  • 51,570
  • 19
  • 123
  • 158
6

You can use just JavaScript:

window.location = 'http://address.com';
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Robert
  • 8,600
  • 2
  • 24
  • 34