3

I want to use javascript to reload a page. I can't just use window.location.reload(); as I'm using jquery tabs and want to specify a tab to be selected after the reload. Is setting window.location.href the best way to do this?

i.e. window.location.href = window.location.href + '#tabs-4';

4
  • have you tried window.location.hash='#tabs-4' ? Commented Feb 18, 2011 at 18:00
  • @amosrivera I assume that will not reload the page though? Commented Feb 18, 2011 at 18:03
  • you can load content for that specific tab by using: jqueryui.com/demos/tabs/#method-load would that work? Commented Feb 18, 2011 at 18:10
  • If you want to load the whole page again, than your method is probably best. Commented Feb 18, 2011 at 19:52

4 Answers 4

1

A cleaner way:

window.location.hash = 'tabs-4';
window.location.reload();
Sign up to request clarification or add additional context in comments.

Comments

0

All you have to do is to pass a var in the url (post) than with your javascript retreive it and select the tab associated to the var

Comments

0

you can have variable in redirect link like index.html?tab=4 and than check it's value in javascript.

function get(name){

if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search)) return decodeURIComponent(name[1]); } you can use this code to get a value. like

selec=get('tab');
 $tabs.tabs('select', selec);

to switch to a tab. See this link to get more details about

How to get "GET" request parameters in JavaScript?

2 Comments

I want to reload the page from within a function if certain conditions are met. I can't just use a link.
but you can generate link on the basic of your condition. if(condition 1 ){ when you want to redirect page and switch to tab 1 link=link+"?tab=1" }
0

As Brandon Joyce commented, my original code was probably best:

window.location.href = window.location.href + '#tabs-4';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.