Open page on tab # 2 using bootstrap

I am linking to a page that uses tabs (Bootstrap). By default it always opens on tab # 1, for the most part this is ok, but for this particular link I want for it to open on tab# 2

This is my link:

links_index.asp?caseid=<%=(rs_case.Fields.Item("Id").Value)%>

This is the code for the tabs:

    <ul class="nav nav-tabs">
      <li class="active"><a data-toggle="tab" href="#tab-1">Users</a></li>
      <li class=""><a data-toggle="tab" href="#tab-2">Sponsoring Employer</a></li>
    </ul>
LVL 1
AleksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
Can you put a hash at the end?

links_index.asp?caseid=<%=(rs_case.Fields.Item("Id").Value)%>#tab-2

Then add some jQuery to switch the tabs on document ready.
$(function(){
	var url = document.location.toString();
	if (url.match('#')) {
		$('.nav-tabs a[href=#'+url.split('#')[1]+']').tab('show') ;
	} 
});

Open in new window

0
AleksAuthor Commented:
It works, moves the page to tab# 2 ... the only problem I find is that it also scrolls the page all the way down, can this be avoided ?
0
Tom BeckCommented:
Sure. Rather than add a hash to the url, add another parameter to indicate what tab you want to open with. Call it "tab".

links_index.asp?caseid=<%=(rs_case.Fields.Item("Id").Value)%>&tab=tab-2

Then use this script:
$(function(){
	$('.nav-tabs a[href=#'+ getParameterByName('tab') +']').tab('show');
});
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

AleksAuthor Commented:
Perfect ! You da man  :)
0
AleksAuthor Commented:
Perfect !
0
Tom BeckCommented:
Glad it worked for you. Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.