105

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.

3
  • 10
    $(location).attr('href','http://address.com'); Commented Aug 16, 2011 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. Commented Aug 16, 2011 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 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. Commented Mar 17, 2020 at 10:40

6 Answers 6

111

There's no need for jQuery.

window.location.href = 'http://example.com';
Sign up to request clarification or add additional context in comments.

Comments

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;

3 Comments

Answer is on this link. Can I add this to my question to find ppl it easily? Is it legal? Or what must i do?
Could mark this case as duplicate of that one?
You fail to explain that the replace makes you unable to navigate back to the current page after the redirect.
29

This is easier:

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

Or

location.replace('http://address.com'); // <-- No history saved.

Comments

13

I think you are looking for:

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

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

2 Comments

No, I was looking for by jquery. I've found.
@jammypeach: jQuery is Javascript. Javascript is not jQuery.
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?

Comments

6

You can use just JavaScript:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.