João Pedro
João Pedro

Reputation: 546

Open bootstrap tabs with AJAX

I want to open bootstrap tabs and instantly show the href page inside each tab onclick. I have this small code below but it does not output any page.

html

<div id="tabs">
    <ul class="nav nav-tabs" id="prodTabs">
        <li class="active"><a href="#tab_basic" data-url="http://localhost/bioinformatica/stuff/pagina.html">Basic</a></li>
        <li><a href="#tab_images" data-url="httpt://localhost/bioinformatica/stuff/pagina.html">pagina</a></li>
        <li><a href="#tab_videos" data-url="www.facebook.com">FB</a></li>
    </ul>
    <div class="tab-content">
        <div id="tab_basic" class="tab-pane active"></div>
        <div id="tab_images" class="tab-pane active"></div>
        <div id="tab_videos" class="tab-pane active"></div>
    </div>
</div>

Javascript

$('#tabs a').click(function (e) {
    e.preventDefault();

    var url = $(this).attr("data-url");
    var href = this.hash;  
    var pane = $(this);

    // ajax load from data-url

    $(href).load(url,function(result){      
        pane.tab('show');
    });
});

// load first tab content
$('#tab_basic').load($('.active a').attr("data-url"),function(result){
  $('.active a').tab('show');
});

Upvotes: 2

Views: 2142

Answers (1)

void
void

Reputation: 36703

You need to prepend http:// in the data-url or else the code will append the domain where it is already running.

Also, even after prepending http:// it is not gonna work because of Cross Domain Request. Dont worry your code is fine, it is just that http://google.com doesn't allow any other to make an AJAX request to it.

Just try it with some file already present on your local server.

Upvotes: 1

Related Questions