I use JQueryUI TABS plugin.
There is my tabs structure.
<div id="tabs">
<ul>
<li><a href='#tabs-1'>TabGroup1</a></li>
<li><a href='#tabs-2'>TabGroup2</a></li>
</ul>
<div id='tabs-1'>
<a href='tab1_link1.html'>TabGroup1-link1</a>
<a href='tab1_link2.html'>TabGroup1-link2</a>
</div>
<div id='tabs-2'>
<a href='tab2_link1.html'>TabGroup2-link1</a>
<a href='tab2_link2.html'>TabGroup2-link2</a>
</div>
</div>
I use such code to select and load first link in tab, when tab is selected. It works itself.
$(function() {
$( "#tabs" ).tabs();
// Activate first link on tab
$( "#tabs" ).bind( "tabsselect", function(event, ui) {
window.location.href = $(ui.panel).find('a:first').attr('href');
});
});
BUT in my task addition code for selecting tab by URL parameters strongly nessesary. So if visitor opened link from second tab-group, second tab must be showed opened, not default first one.*
I have working code which show correct tab when link from this tab is loaded (no AJAX or such, just ussual links). $URL['part']
is variable I recieve from my URL in engine, this works fine separately.
<?php if(@$URL['part']=='part2'){
echo '<script>$(function() { $("#tabs").tabs("select","#tabs-2"); });</script>';
} ?>
BUT when I use both these code-blocks it cause page repeatedly infinite reload :-(
UPDATED:
Notice, that both of blocks of code use SELECT event, that why the looping occurs.
UPDATED2:
I suppose, if use ONCLICK for loading link when tab is selected, and SELECT on activation tab due to URL settings, it can solve the problem. But I don't know how to write this in code.
UPDATED:
This solution isn't correct! I keep it for historical reasons and comments are below it. https://stackoverflow.com/a/10642137/1368570 - this is working correct solution: clear and simple
I have found solution very close to task:
Code to acrivate propper TAB basing on the URL parameter processed in my engine
PROBLEM:
When I go from 1-st tab to 2-d or 3-d, window.location.href don't work, URL dont changes.
If I go from any tab except frist, all works perfectly.
I am too tired today to find solution to this issue, if any one have ideas, will be glad to hear them!
Also, when I uncomment alert in my code, I see that selected var is [object Object]. So if I compare object with non-object, why this complete solution works well in most cases?
Does this work if you move the processing inside the tabs creation:
In a study of you code, it might be better to check the URL and not go anywhere if it is the same page, either by link OR by tab select:
In reference to your comment, there is a create handler so you would have the bind inside that:
but I am unsure of the event firing on the creation sequence, and if the select of the tab occurs before or after the create event (I would think after) as I have not tested this.
If I read it right, then you will be having same code in the HTMLs in the link, i.e.
and you will be calling the
$( "#tabs" ).tabs()
on that page. IF so, why not use$( "#tabs" ).tabs({ active: 1 })
in the page where the link is supposed to redirect?This is complete final solution I found, which WORKS (crossbrowser)!
Creates tabs
Select the tab which must be active for the current URL we have
The below code MUST be after we started tabs and selected which one is now active, otherwise we will recieve infinite url-reloading!
So, when I moved tab selection based on URL to the begining of code, this solved all the issues and make solution this simple!