coyotee
coyotee

Reputation: 11

The script I'm using to redirect my homepage isn't working the way I need it to

I'm using the code below to redirect my Tumblr homepage to my latest post.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
   $.getJSON('http://mydomain.tumblr.com/api/read/json?callback=?', function(result) {
      window.location.href = result.posts[0].url;
   });
});
</script>

I'm having two problems with it. First, it doesn't redirect instantly, it loads my homepage and then changes the URL, even though I've placed it above my <head> tag. Then when it does redirect it starts refreshing every few seconds. Thanks in advance for any help.

Upvotes: 1

Views: 155

Answers (2)

Draykos
Draykos

Reputation: 822

It redirects after you load the page because code in $(document).ready(function(){ ...}); is executed after page load. To avoid refresh after redirect, try something like:

if(window.location.href !== result.posts[0].url) {
   window.location.href = result.posts[0].url;
}

Upvotes: 0

Musa
Musa

Reputation: 97672

The reason why it waits is because any code you place in $(document).ready waits for the for the document to load first before it is executed.

For the refreshing part, I guess you have this same code in your page with the lastest post.

Upvotes: 2

Related Questions