Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Chrome redirect issue

Hello all,

I`m trying to use a redirect function but it is not working on Chrome !

I try to use these functions :

1.(location).attr
2.(location).href
3.(location).assign
4.window.location.assign(url);

any Idea?

Thank you.

6 Answers

Try using: window.location.href = "mynewurl.com"

@Jonas

Thank you for answering, but it`s not working also.

No problem Omar! Well, if you are referring to javascript, it should work :)

Here's a quick example where I have declared a function, which will be called after 5 seconds (redirect you to treehouse):

function sendMeToTreehouse(){ window.location.href = "http://www.teamtreehouse.com" }

setTimeout(function(){ sendMeToTreehouse(); }, 5000);

I've created a working jsfiddle that you can have a look at: http://jsfiddle.net/z36PJ/

@Jonas

Thank you so much man, I find the problem its not from the redirect, but it`s something weird,

before the javascript code I write the $(document).ready(function(){ //Some code here }); after that I put the redirect code then Whoops its not working, if I remove the .ready fucnction then it will work fine.

Thank you again :)

If you take a look at the developer console in chrome (developer tools > console), do you get any error messages?

Placing code inside a $(document).ready function will execute the code once the DOM is ready (when all the html has been loaded, this does not include loading images and so on, just parsing the actual code).

Placing it outside the $(document).ready(function(){}); will execute the code straight away (once the browser has read the code).

If you wish to execute something after the browser is done loading everything (including images) then you can use the $(window).load(function(){});.

Heres a quick example of the order in which the code will be executed:

$(document).ready(function(){ alert('This will execute second'); });

$(window).load(function(){ alert('This will execute last'); });

alert('This will execute first');

@Jonas,

Thank you so much for your care, actually the problem solved, and thank you for help.