• TabGroup1
  • TabGroup2<" />
    • TabGroup1
    • TabGroup2<" />
      • TabGroup1
      • TabGroup2<"/>
        DEVHIDE
        • Home (current)
        • About
        • Contact
        • Cookie
        • Home (current)
        • About
        • Contact
        • Cookie
        • Disclaimer
        • Privacy
        • TOS
        Login Or Sign up

        Selection tab and onselect code conflict - infinite reloading of page

        1.5k Views Asked by DaneSoul At 16 May 2012 at 16:13 2025-12-20T22:54:28.003000

        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 best solutions below

        0
        DaneSoul DaneSoul On 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 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 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 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

        • How to sort these using Javascript or Jquery Most effectively
        • Ajax jQuery firing multiple time display event for the same result
        • .hover() seems to overwrite .click()
        • Check for numeric value with optional commas javascript
        • Extending Highmaps Side Effect
        • Array appending after each onclick and loop in javascript
        • how can i append part of a table based on how many tr it has?
        • Play multiple audio files in a slider
        • Remove added set of rows
        • Access property of an object of type [Model] in JQuery
        • AJAX PHP - Reload div after submit
        • proengsoft/laravel-jsvalidation ReferenceError: jQuery is not defined
        • when a checkbox is checked how to display a different hidden element using javascript
        • Get jquery error Uncaught RangeError: Maximum call stack size exceeded
        • Removing only the closest thead on table filtering

        Related Questions in JQUERY-UI

        • Uncaught TypeError: undefined is not a function
        • Stop siblings replacing order of canceled element with jQuery-ui
        • jQuery mobile Multiselect doesn't update selected attribute
        • jQuery autocomplete display worng search item
        • List of conflicts between bootstrap and jquery-ui?
        • Creating boundries in JQuery/javascript
        • Having trouble focusing on the TAB that I was currently working on
        • How can i show a jquery menu which opens from right to left side?
        • Can't toggle from right to left Jquery
        • undefined new items appear while reordering a list with ui-sortable and angularJS
        • Jquery Mobile: Horizontal Buttons, fill container
        • Is it possible to override defined options of the jQuery-ui sortable widget?
        • Quick way to clear only option available list on a multiselect with jQuery?
        • How to inject html into iframe, and display inside jquery-ui dialog
        • Show embedded select box on overflow, out of popup container

        Related Questions in JQUERY-UI-TABS

        • Having trouble focusing on the TAB that I was currently working on
        • jQuery tabs enable horizontal scroll
        • Tabs JQuery UI: not show graphic elements in inactive tab
        • Multiple jQuery UI Tabs on Page: Close all other tabs when another is opened
        • jQuery tabs: How to avoid display of tabs while loading?
        • JQUERY UI TABS - PreventDefault jumping
        • oscMax jquery nested tabs issue
        • How to make element inside table switch tabs using jquery ui tabs
        • Tabs, Hide Previous on first tab and next on last
        • How to trigger tab using jQuery
        • jquery-ui tabs getting anchor href with older jquery 1.8
        • External file does not load in jQuery Tabs
        • How to pre-load tab content (other view with table and data loaded from DB using Zend controllers), before displaying tabs ?
        • jQuery UI Tabs: Don't let user switch tabs if form input in active tab has content
        • Dynamic Jquery Tabs based on Php foreach

        Related Questions in CONFLICT

        • Reuse jquery plugin without conflict
        • GIT Source Tree Merge Develop Branch into Stage Branch Conflict
        • CouchDB Merging Revisions (conflict)
        • javascript function conflict or css select conflict
        • find the number of all possible combinations with conflicts
        • CONFLICT (content): Merge conflict in
        • Skrollr - any other jQuery animation conflict
        • Nginx Subdomain/Staging Server conflicting server name
        • Conflict issue when multi developers work on same task
        • Composer avoid installing sub-dependency
        • Run this JS on selected blog posts types (Tumblr)
        • How to resolve Deleted merge conflict in TortoiseGit?
        • c++ Binary tree conflicting types error
        • Stop execution until User selects a button from the Alert Dialog
        • Conflicting components Joomla

        Related Questions in ONSELECT

        • Android - Advanced Usage for Spinner.OnItemSelectedListener
        • JQuery Datepicker - onSelect Date Format
        • How can I pass a variable into a Date picker onSelect function
        • jQuery DatePicker OnSelect Event Fired too early
        • How to add both server-side and client-side event handler in RichFaces
        • Click Listener on ListView
        • Display data by selecting year only using onChangeMonthYear
        • Printing out to a label using OnSelectChange in gridview
        • Adding data using on select
        • Onchange event of Select Control in Blazor app (net8) is not firing
        • react mui/x-data-grid onSelectionChange - is not tracked / invoked
        • Selection tab and onselect code conflict - infinite reloading of page
        • split the source of images depending upon selection
        • How to call a js function onSelect event of datepicker jquery?
        • how do I create a jQuery nTier select compare script?

        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)

        Popular # Hahtags

        javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

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

        Copyright © 2021 Jogjafile Inc.

        • Disclaimer
        • Privacy
        • TOS
        • Homegardensmart
        • Math
        • Aftereffectstemplates