• TabGroup1
  • TabGroup2<" />
    • TabGroup1
    • TabGroup2<" />
      • TabGroup1
      • TabGroup2<"/>

        TechQA.

        Selection tab and onselect code conflict - infinite reloading of page

        1.5k views Asked by DaneSoul At 2012-05-16T16:13:31+00:00 16 May 2012 at 16:13 2025-12-21T09:58:52+00:00

        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.

        jquery jquery-ui jquery-ui-tabs conflict onselect
        Original Q&A
        4

        There are 4 answers

        0
        DaneSoul DaneSoul On 2012-05-17T19:17:57+00:00 17 May 2012 at 19:17 BEST ANSWER

        This is complete final solution I found, which WORKS (crossbrowser)!

        Creates tabs

        $(function() {
            $( "#tabs" ).tabs();
        });
        

        Select the tab which must be active for the current URL we have

        <?php 
        if(@$URL['part']=='part1'){
            echo '$(function() { $("#tabs").tabs("select","#tabs-1"); });'; 
        } 
        if(@$URL['part']=='part2'){
            echo '$(function() { $("#tabs").tabs("select","#tabs-2"); });'; 
        } 
        ?>
        

        The below code MUST be after we started tabs and selected which one is now active, otherwise we will recieve infinite url-reloading!

        $(function() {
            $('#tabs').tabs({
                select: function(event, ui) {
                    window.location.href = $(ui.panel).find('a:first').attr('href');
                }
            });
        });
        

        So, when I moved tab selection based on URL to the begining of code, this solved all the issues and make solution this simple!

        5
        Mark Schultheiss Mark Schultheiss On 2012-05-16T16:59:54+00:00 16 May 2012 at 16:59

        Does this work if you move the processing inside the tabs creation:

        $('#tabs').tabs({
            select: function(event, ui) {
                window.location.href = $(ui.panel).find('a:first').attr('href');
            }
        });
        

        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:

        $('#tabs').tabs({
            select: function(event, ui) {
                checkUrl(event, ui.panel);
            }
        });
        // handle link click on current tab
        $('div>a').click(function(event) {
            checkUrl(event, event.target);
        });
        
        function checkUrl(event, ui) {
            var ehref = $(event.target).attr('href');
            var wl = window.location.href;
            var currentLocation = typeof ehref == "undefined" ? wl: ehref;
            var newLocation = $(ui.panel).find('a:first').attr('href');
            if (currentLocation != newLocation) {
                window.location.href = newLocation;
            }
        };
        

        In reference to your comment, there is a create handler so you would have the bind inside that:

        $('#tabs').tabs({
            create: function(event, ui) {
               $( "#tabs" ).bind( "tabsselect", function(event, ui) {
                   window.location.href = $(ui.panel).find('a:first').attr('href');
               });
            }
        });
        

        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.

        4
        DaneSoul DaneSoul On 2012-05-16T23:43:49+00:00 16 May 2012 at 23:43

        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:

        <script language="JavaScript" type="text/javascript">
        function MySelect(event, ui){
            // selected - currently selected tab (string)
            var clicked_tab = '' + ui.index //clicked tab
        
            if(selected !== clicked_tab ){
                //So this code will works ONLY when another tab is selected, so no infinite loop        
                //alert(selected+'/'+ui.index);
        
                    // Activate first link on tab
               $( "#tabs" ).bind( "tabsselect", function(event, ui) {
                 window.location.href = $(ui.panel).find('a:first').attr('href');
               });
            }
        }   
        
        $(function() {
            $('#tabs').tabs({
                select: function(event, ui) {
                    MySelect(event, ui);
                }
             });
        });
        
                //retrieve the index of the currently selected tab
        var tabs = $('#tabs').tabs();
        var selected = '' + tabs.tabs('option', 'selected'); // from zero 0
        </script>
        

        Code to acrivate propper TAB basing on the URL parameter processed in my engine

        <?php 
        if(@$URL['part']=='part1'){
            echo '<script>$(function() { $("#tabs").tabs("select","#tabs-1"); });</script>';    
        } 
        if(@$URL['part']=='part2'){
            echo '<script>$(function() { $("#tabs").tabs("select","#tabs-2"); });</script>';    
        } 
        if(@$URL['part']=='part3'){
            echo '<script>$(function() { $("#tabs").tabs("select","#tabs-3"); });</script>';    
        }
        ?>
        

        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?

        0
        Tiquelou Tiquelou On 2013-05-23T12:45:48+00:00 23 May 2013 at 12:45

        If I read it right, then you will be having same code in the HTMLs in the link, i.e.

        <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>
        

        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?

        Related Questions in JQUERY

        • In Datatables, start value resets to 0, when column sorting
        • Bootstrap modal not showing at the desired position on a web page when the screen size is smaller
        • window.location.href redirects but is causing problems on the webpage
        • Using JQuery Date Slider
        • Storing selected language in localStorage
        • How to stop other divs from still showing when i click a different button?
        • Check multiple values with jQuery
        • Bootstrap component does not want to render in Datatables function
        • put white spaces when entering an amount moneytype symfony
        • Trouble accessing custom header in AJAX response using jQuery in Fiware Keyrock
        • I just cant make it work, HTML, JS and Firebase error
        • Didn't declared variable still not getting any error in JavaScript
        • Move element horizontally while scrolling vertically in pure JavaScript
        • allow multi carousel in same page
        • Embedded TikTok posts / thumbnail styling issue

        Related Questions in JQUERY-UI

        • jquery__WEBPACK_IMPORTED_MODULE_13__(...).sortable is not a function
        • Jquery autopopulate not displaying
        • jQuery plugin is not shown
        • How to upgrade blueimp jquery file upload to use jquery-widget 1.13
        • Set value JQUERY UI Slider with LeaFlet map
        • trying to delete existing tabs which were dynamically added
        • Jquery UI sortable addon with React not working
        • Angular.js directive does not work for uib-tooltip
        • tooltip for input fields not loading
        • How to install jq.Schedule in Vue3 + Typescript
        • JQuery - Drag, Drop and Sortable
        • tabs getting adding again and again
        • JQuery UI remove tooltip on successful input
        • jQuery Autocomplete Firing to Wrong URL
        • How to add total count in centre of the doughnut chart and bottom left and right value using Chart.js?

        Related Questions in JQUERY-UI-TABS

        • Dynamically created jQuery Tabs on ColdFusion Pages
        • jquery tabs sometimes set the iframe height to zero
        • How to activate the parent tab content using javascript?
        • jQuery UI tabs using aria-controls instead of title to have a single panel
        • Remember last clicked tab on page refresh
        • Uncaught Error: Method load does not exist on jQuery.tabs
        • How to integrate JqueryUI Tabs in Blazor webAssembly application
        • Load Php Page Inside Jquery Chrome Tabs
        • Jquery tab navigation not working properly
        • jquery-ui tab "Interactive controls must not be nested"
        • Jquery tab not showing first child selected
        • Reactjs Dynamically call components having cards under tabpanels using array
        • How to manipulate Bootstrap tabs from another page using Ajax
        • link to a page with tabbed content doesnt work
        • I want a single page scroll with top nav menu scroll and active (Active menu should be in first position scroll)

        Related Questions in CONFLICT

        • Yocto conflicting error while building rootfs
        • How to resolve a merge conflict in Git?
        • Dependency Conflict with TOCropViewController in React Native Project
        • R package building: importing functions with identical names
        • i want to use im my requirements.txt this tensorflow~=2.3.0 version but i get an error
        • Merge happens in reverse way while using GitHub
        • Auto-conflict detection and resolution in Maria DB
        • Remove git merge commit without conflict
        • How to set utf-8 as default encoding for pull request merge conflict extension in azure devops?
        • How to resolve version conflict for Parselmouth and Pandas during installation of pytz?
        • AlpineJS conflict x-data with VueJS
        • Ionic Framework generate and Angular ESLint 02-03 styleguide rule component naming conflict
        • Ignoring files that are not present in the destination branch when merging a source branch into it with git
        • Tailwind background utility class not working in Next.js(pages router)
        • How to support "FULL JOIN" clause

        Related Questions in ONSELECT

        • react mui/x-data-grid onSelectionChange - is not tracked / invoked
        • Onchange event of Select Control in Blazor app (net8) is not firing
        • React Input onSelect Component shows Previous Selection
        • react drag drop files re-upload issue in chrome
        • Trigger react select onchange
        • Multiple Sets in PowerApps OnSelect doesn't work
        • Selected Tab is not highlighted in the Navbar in reactjs bootstrap?
        • How to create a shopify variable using jQuery datepicker that can then be rendered in email templates?
        • add text area on selecting options in dropdown list in java script
        • Select on change event disappear after tr modification
        • react - onselect change display previous value
        • Auto select the first match in react auto complete
        • oncomplete for p:ajax event is always called, even if the listener returns nothing
        • Why is the first item in the Spinner not working
        • How can you display two event items in an alert for react-big-calander onSelectEvent

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)
        • Privacy
        • Terms
        • Cookies
        • Homegardensmart
        • Aftereffectstemplates
        • Jogjafile