Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening JQuery tabs with URL, and adding a hash to URL on tab click

I'm working on a web application, and I am using the JQuery UI Tabs plugin to separate the data.
If I mouse over each one of the tabs, on the lower left of the screen I can see the URL of that tab (ex: testPage.com/#tab1 or testPage.com/#tab2)

Now, if I type in one of those addresses into the URL bar, I do not go to the tab related to the hash in the URL, but instead I go to the first tab in the application.

My question is: How would I go about anchoring a specific tab to a URL?
So if someone goes to testPage.com/#tab3, They will end up in tab 3 on the application.

I am doing this also because I want to use the JQuery BBQ plugin to modify the browser history, so I can have a user go to the last tab they were on when they hit the back button.

Example:
http://benalman.com/code/projects/jquery-bbq/examples/fragment-jquery-ui-tabs/

like image 860
npiani Avatar asked Jun 13 '12 21:06

npiani


People also ask

How to open link in New Tab in jQuery?

Let's get started with jquery open link in new tab on button click. I created two way to open link in new tab in jquery. first one is a using window.open () and another is 'target="_blank"' on anchor tag. so i added both example bellow you have to just check that you can see how it will helps us.

How to open a resource in a new tab using JavaScript?

Another plausible way to open a referenced resource in a new tab is to dynamically create an anchor tag with the href attribute set to the resource URL and the target attribute set to _blank for making it open in a new tab. After the object is created, don’t attach it to any DOM objects but trigger a click on it using JavaScript’s click () method.

How do I force a browser to open a new tab?

Although the tab-capable browsers prefer opening new tabs to opening new windows, you can explicitly force this behavior by passing the _blank target to the open () method. Note that some browsers like Google Chrome will automatically block pop-ups from showing up on your screen.


2 Answers

I ended up using the JQuery Address plugin from Asual. I did the following:

    // For forward and back
    $.address.change(function(event){
        $("#main_tabs").tabs( "select" , window.location.hash );
    });

    // when the tab is selected update the url with the hash
    $("#main_tabs").bind("tabsselect", function(event, ui) { 
        window.location.hash = ui.tab.hash;
    });

Hopefully this helps someone! Thank you.

like image 72
npiani Avatar answered Oct 02 '22 16:10

npiani


You can also try in the following way. I have name property in the anchor element. This name will be added as hash URL.

Javascript:

$( "#tabs" ).tabs({
    select: function(event, ui) {                   
        window.location.hash = ui.tab.hash;                     
    }
});

HTML:

<div id="tabs" > 
    <ul>
        <li><a name="Test1" href="Home/Test1"> Tab 1  </a></li>
        <li><a name="Test2" href="Home/Test2"> Tab 2  </a></li>
        <li><a name="Test3" href="Home/Test3"> Tab 3  </a></li>            
    </ul>        
</div>
like image 43
N Rocking Avatar answered Oct 02 '22 17:10

N Rocking