How to refresh iframe when click on relevant tab using javascript? - javascript

I'm using Three iframe inside index files with 3 Tabs. I want to load or refresh the files inside the iframe when each tab is selected.
Here is my index files
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a data-target="#home" data-toggle="tab">Enter</a></li>
<li><a data-target="#profile" data-toggle="tab" >Display</a></li>
<li><a data-target="#messages" data-toggle="tab">Search</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="home">
<iframe src="EnterBook.php" ></iframe>
</div>
<div class="tab-pane" id="profile" >
<iframe src="DisplayBooks.php" ></iframe>
</div>
<div class="tab-pane" id="messages">
<iframe src="SearchBooks.php" ></iframe>
</div>
</div>
I did look over other solution but nothing helped.. Please anyone can have a look over it.

You need to add click event handler to every tab and in callback function get relevant iframe of clicked tab and change reset src attribute of it or use Location.reload()
document.querySelectorAll('#myTab a').forEach(function(ele){
ele.onclick = function(){
var iframe = document.querySelector(this.dataset.target+'>iframe');
iframe.src = iframe.src;
}
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<ul class="nav nav-tabs" id="myTab">
<li class="active">
<a data-target="#home" data-toggle="tab">Enter</a>
</li>
<li>
<a data-target="#profile" data-toggle="tab" >Display</a>
</li>
<li>
<a data-target="#messages" data-toggle="tab">Search</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="home">
<iframe src="https://stacksnippets.net/"></iframe>
</div>
<div class="tab-pane" id="profile">
<iframe src="https://stacksnippets.net/"></iframe>
</div>
<div class="tab-pane" id="messages">
<iframe src="https://stacksnippets.net"></iframe>
</div>
</div>

Related

How to change the URL of an anchor tag based on current active tab?

I have two tabs, Google and Yahoo!. The goal is that if the 'Google' tab is active, then the plus sign icon (on the right) will contain the URL based on the conditional.
Example: If 'Google' tab is active, then clicking on the plus sign should open a google.com page.
I'm not sure what I'm doing wrong:
let activeTab = document.querySelector(".nav-tabs li.active");
let selectedForm = document.querySelector("#formSelector");
if (activeTab.textContent == "Google") {
selectedForm.setAttribute("onclick", "location.href='https://www.google.com'");
} else if (activeTab.textContent == "Yahoo!") {
selectedForm.setAttribute("onclick", "location.href='https://www.yahoo.com'");
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="container">
<h2>Dynamic Tabs</h2>
<ul class="nav nav-tabs">
<li class="active">
<a data-toggle="tab" href="#google">Google</a>
</li>
<li>
<a data-toggle="tab" href="#yahoo">Yahoo!</a>
</li>
<li class="pull-right">
<a id="formSelector" href="#!" target="_blank"><i class="fa fa-plus"></i></a>
</li>
</ul>
<div class="tab-content">
<div id="google" class="tab-pane fade in active">
<h3>Google</h3>
<p>This should change the plus sign icon to google.com</p>
</div>
<div id="yahoo" class="tab-pane fade">
<h3>Yahoo!</h3>
<p>This should change the plus sign icon to yahoo.com</p>
</div>
</div>
</div>
I would recommend adding a click event listener on the button that checks the active tab and redirects accordingly:
let selectedForm = document.querySelector("#formSelector");
selectedForm.addEventListener('click', function() {
let activeTab = document.querySelector(".nav-tabs li.active");
if (activeTab.textContent.trim() == "Google") {
location.href = 'https://google.com'
} else {
location.href = 'https://yahoo.com'
}
})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="container">
<h2>Dynamic Tabs</h2>
<p>To make the tabs toggleable, add the data-toggle="tab" attribute to each link. Then add a .tab-pane class with a unique ID for every tab and wrap them inside a div element with class .tab-content.</p>
<ul class="nav nav-tabs">
<li class="active">
<a data-toggle="tab" href="#google">Google</a>
</li>
<li>
<a data-toggle="tab" href="#yahoo">Yahoo!</a>
</li>
<li class="pull-right">
<a id="formSelector" href="#!" target="_blank"><i class="fa fa-plus"></i></a>
</li>
</ul>
<div class="tab-content">
<div id="google" class="tab-pane fade in active">
<h3>Google</h3>
<p>This should change the plus sign icon to google.com</p>
</div>
<div id="yahoo" class="tab-pane fade">
<h3>Yahoo!</h3>
<p>This should change the plus sign icon to yahoo.com</p>
</div>
</div>
</div>
As Spectric said, moving the check to be based on click of a tab would be ideal.
Here's an alternative implementation (requires HTML & JS changes).
HTML
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="container">
<h2>Dynamic Tabs</h2>
<p>To make the tabs toggleable, add the data-toggle="tab" attribute to each link. Then add a .tab-pane class with a unique ID for every tab and wrap them inside a div element with class .tab-content.</p>
<ul class="nav nav-tabs">
<li class="active">
<a data-toggle="tab" href="#google" related-url="https://google.com">Google</a>
</li>
<li>
<a data-toggle="tab" href="#yahoo" related-url="https://yahoo.com">Yahoo!</a>
</li>
<li class="pull-right">
<a id="formSelector" href="#!" target="_blank"><i class="fa fa-plus"></i></a>
</li>
</ul>
<div class="tab-content">
<div id="google" class="tab-pane fade in active">
<h3>Google</h3>
<p>This should change the plus sign icon to google.com</p>
</div>
<div id="yahoo" class="tab-pane fade">
<h3>Yahoo!</h3>
<p>This should change the plus sign icon to yahoo.com</p>
</div>
</div>
</div>
Javascript
<script>
const tabClick = document.querySelectorAll(".nav-tabs");
const selectedForm = document.querySelector("#formSelector");
tabClick.forEach((tabEl) => {
tabEl.addEventListener('click', (e) => {
console.log(tabEl);
console.log(e);
selectedForm.setAttribute('href', e.target.getAttribute('related-url'));
});
})
</script>

Bootstrap Tab pills not working or linking with tab panel

I have created a bootstrap pills in my website but its is not working the way its showed on documentation. when I click on the pill its not opening the specific tab panel. here am attaching the code.Here am tring to add tabs pills linking with specific contents below in divs. Don't know what am missing in the code but when i click on the tab the contents are not changing.
<script>
$(".nav li a").on("click", function() {
$(".nav li a").removeClass("active");
$(this).addClass("active");
});
</script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<main role="main" class="container">
<div class="text-center mt-5 pt-5">
<h1>Verbal Ability :: Comprehension</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
<!--Test Links-->
<div class="overflow-auto bg-light" style="height: 200px;">
<ol class="nav nav-pills flex-column">
<li class="nav-item">
<a class="nav-link active" href="#home" data-toggle="pill">Comprehension - Section 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#profile" data-toggle="pill">Comprehension - Section 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" data-toggle="pill">Comprehension - Section 3</a>
</li>
</ol>
</div>
<div class="tab-content">
<div id="home" class="tab-pane fade show active" role="tabpanel">Content 1</div>
<div id="profile" class="tab-pane fade" role="tabpanel">Content 2</div>
</div>
</main><!-- /.container -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
You should load jQuery before loading Bootstrap. Please read the docs properly.
Your script method of adding a class("active") was wrong. There is an inbuilt method provided by Bootstrap .tab("show") which then enables the tab to toggle properly as per the given ID's
I have changed the snippet and it's working now. Kindly go through this https://www.w3schools.com/bootstrap/bootstrap_ref_js_tab.asp to learn more in detail.
$(document).ready(function(){
$(".nav-item a").click(function(){
$(this).tab('show');
});
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<main role="main" class="container">
<div class="text-center mt-5 pt-5">
<h1>Verbal Ability :: Comprehension</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
<!--Test Links-->
<div class="overflow-auto bg-light" style="height: 200px;">
<ol class="nav nav-pills flex-column">
<li class="nav-item">
<a class="nav-link active" href="#home" data-toggle="pill">Comprehension - Section 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#profile" data-toggle="pill">Comprehension - Section 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#third" data-toggle="pill">Comprehension - Section 3</a>
</li>
</ol>
</div>
<div class="tab-content">
<div id="home" class="tab-pane fade show active" role="tabpanel">Content 1</div>
<div id="profile" class="tab-pane fade" role="tabpanel">Content 2</div>
<div id="third" class="tab-pane fade" role="tabpanel">Content 3</div>
</div>
</main><!-- /.container -->
<!-- jQuery should come before loading boostrap -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

Bootstrap tab dropdown doesn't show after 1st click

In my sample code below, the dropdown tab contents does not show after it's viewed for first time.
(function($) {
$(function() {
$('a[data-toggle = "tab"]').on('shown.bs.tab', function(e) {
// Get the name of active tab
var activeTab = $(e.target).text();
// Get the name of previous tab
var previousTab = $(e.relatedTarget).text();
$(".active-tab span").html(activeTab);
$(".previous-tab span").html(previousTab);
});
});
})(jQuery);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<main class="main"></main>
<hr>
<p class="active-tab"><strong>Active Tab</strong>: <span></span></p>
<p class="previous-tab"><strong>Previous Tab</strong>: <span></span></p>
<hr>
<ul class="nav nav-tabs" role="tablist" id="myTab">
<li role="presentation" class="active">Home</li>
<li role="presentation">Details</li>
<li class="dropdown">
<a href="#" id="myTabDrop1" class="dropdown-toggle" data-toggle="dropdown">
Java
<b class="caret"></b>
</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li>
D1
</li>
<li>
D2
</li>
</ul>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<p>This is the home page</p>
</div>
<div role="tabpanel" class="tab-pane" id="account-details">
<p>Pane 1</p>
</div>
<div role="tabpanel" class="tab-pane" id="d1">
<p>Dropdown 1</p>
</div>
<div role="tabpanel" class="tab-pane" id="d2">
<p>Dropdown 2</p>
</div>
</div>
</main>
<!-- Page style-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
When the active tab becomes one of the dropdown values (D1/D2) and when you change the active tab to any other tab, the Previous Tab: value becomes empty.
Now if I select dropdown 1 again, I don't see the content.
Any feedback is appreciated.
Thank you.
You are mixing up between Bootstrap v3 and v4 which causing this conflict, just remove the v4 one, (Your code is compatible with v3)
(function($) {
$(function() {
$('a[data-toggle = "tab"]').on('shown.bs.tab', function(e) {
// Get the name of active tab
var activeTab = $(e.target).text();
// Get the name of previous tab
var previousTab = $(e.relatedTarget).text();
$(".active-tab span").html(activeTab);
$(".previous-tab span").html(previousTab);
});
});
})(jQuery);
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Main -->
<main class="main">
<hr>
<p class="active-tab"><strong>Active Tab</strong>: <span></span></p>
<p class="previous-tab"><strong>Previous Tab</strong>: <span></span></p>
<hr />
<ul class="nav nav-tabs" role="tablist" id="myTab">
<li role="presentation" class="active">Home</li>
<li role="presentation">Details</li>
<li class="dropdown">
Java<b class="caret"></b>
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li>
D1
</li>
<li>
D2
</li>
</ul>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<p>This is the home page</p>
</div>
<div role="tabpanel" class="tab-pane" id="account-details">
<p>Pane 1</p>
</div>
<div role="tabpanel" class="tab-pane" id="d1">
<p>Dropdown 1</p>
</div>
<div role="tabpanel" class="tab-pane" id="d2">
<p>Dropdown 2</p>
</div>
</div>
</main>
<!-- End main -->

Bootstrap toggleable tab menu pushing content down

I am trying to implement a toggleable tab menu through bootstrap and also using Material Design for bootstrap
I have a issue with the tabs after the first one always pushing its content down (below where the 1st tab content would be)
I have searched around and found a similar issue discussed here and also tried the solution provided but it didn't seem to work for me
$('#toggle-tabs a').click(function(e) {
e.preventDefault()
$(this).tab('show')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript">
</script>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,700' rel='stylesheet' type='text/css'>
<script src="https://use.fontawesome.com/abacba2780.js">
</script>
<div class="container-fluid">
<!-- Content -->
<div class="row">
<!-- Nav tabs -->
<div class="col-sm-2">
<ul class="nav nav-tabs md-pills pills-secondary flex-column" role="tablist">
<li class="nav-item elegant-color-dark">
<a class="nav-link active" data-toggle="tab" href="#test1" role="tab"><br>
<h3>Test1</h3>
</a>
</li>
<li class="nav-item elegant-color-dark">
<a class="nav-link" data-toggle="tab" href="#test2" role="tab"><br>
<h3>Test2</h3>
</a>
</li>
<li class="nav-item elegant-color-dark">
<a class="nav-link" data-toggle="tab" href="#test3" role="tab"><br>
<h3>Test3</h3>
</a>
</li>
</ul>
</div>
</div>
</div>
<!-- Tab panels -->
<div class="tab-content vertical col-sm-10" id="toggle-tabs">
<!--Panel 1-->
<div class="tab-pane fade in show active" id="test1" role="tabpanel">
<h3>Test1</h3>
</div>
<!--Panel 2-->
<div class="tab-pane fade in" id="test2" role="tabpanel">
<h3>Test2</h3>
</div>
<!--Panel 3-->
<div class="tab-pane fade in" id="test3" role="tabpanel">
<h3>Test3</h3>
</div>
</div>
<link href="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.1/css/mdb.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.1/js/mdb.min.js" type="text/javascript">
</script>
You need to remove the show class from the default active tab, so tab-pane fade in show active becomes tab-pane fade in active:
$('#toggle-tabs a').click(function(e) {
e.preventDefault()
$(this).tab('show')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js">
</script>
<!-- Latest compiled and minified CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- Optional theme -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript">
</script>
<script src="main.js" type="text/javascript">
</script>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,700' rel='stylesheet' type='text/css'>
<script src="https://use.fontawesome.com/abacba2780.js">
</script>
</head>
<body>
<div class="container-fluid">
<!-- Content -->
<div class="row">
<!-- Nav tabs -->
<div class="col-sm-2">
<ul class="nav nav-tabs md-pills pills-secondary flex-column" role="tablist">
<li class="nav-item elegant-color-dark">
<a class="nav-link active" data-toggle="tab" href="#test1" role="tab"><br>
<h3>Test1</h3>
</a>
</li>
<li class="nav-item elegant-color-dark">
<a class="nav-link" data-toggle="tab" href="#test2" role="tab"><br>
<h3>Test2</h3>
</a>
</li>
<li class="nav-item elegant-color-dark">
<a class="nav-link" data-toggle="tab" href="#test3" role="tab"><br>
<h3>Test3</h3>
</a>
</li>
</ul>
</div>
</div>
</div>
<!-- Tab panels -->
<div class="tab-content vertical col-sm-10" id="toggle-tabs">
<!--Panel 1-->
<div class="tab-pane fade in active" id="test1" role="tabpanel">
<h3>Test1</h3>
</div>
<!--Panel 2-->
<div class="tab-pane fade in" id="test2" role="tabpanel">
<h3>Test2</h3>
</div>
<!--Panel 3-->
<div class="tab-pane fade in" id="test3" role="tabpanel">
<h3>Test3</h3>
</div>
</div>
<link href="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.1/css/mdb.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.1/js/mdb.min.js" type="text/javascript">
</script>
</body>
</html>
Remove the show class from the first tab pane
<div class="tab-pane fade in show active" id="test1" role="tabpanel">
<h3>Test1</h3>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js">
</script>
<!-- Latest compiled and minified CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- Optional theme -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript">
</script>
<script src="main.js" type="text/javascript">
</script>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,700' rel='stylesheet' type='text/css'>
<script src="https://use.fontawesome.com/abacba2780.js">
</script>
</head>
<body>
<div class="container-fluid">
<!-- Content -->
<div class="row">
<!-- Nav tabs -->
<div class="col-sm-2">
<ul class="nav nav-tabs md-pills pills-secondary flex-column" role="tablist">
<li class="nav-item elegant-color-dark">
<a class="nav-link active" data-toggle="tab" href="#test1" role="tab"><br>
<h3>Test1</h3>
</a>
</li>
<li class="nav-item elegant-color-dark">
<a class="nav-link" data-toggle="tab" href="#test2" role="tab"><br>
<h3>Test2</h3>
</a>
</li>
<li class="nav-item elegant-color-dark">
<a class="nav-link" data-toggle="tab" href="#test3" role="tab"><br>
<h3>Test3</h3>
</a>
</li>
</ul>
</div>
</div>
</div>
<!-- Tab panels -->
<div class="tab-content vertical col-sm-10" id="toggle-tabs">
<!--Panel 1-->
<div class="tab-pane fade in active" id="test1" role="tabpanel">
<h3>Test1</h3>
</div>
<!--Panel 2-->
<div class="tab-pane fade in" id="test2" role="tabpanel">
<h3>Test2</h3>
</div>
<!--Panel 3-->
<div class="tab-pane fade in" id="test3" role="tabpanel">
<h3>Test3</h3>
</div>
</div>
<link href="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.1/css/mdb.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.3.1/js/mdb.min.js" type="text/javascript">
</script>
</body>
</html>

Load iframes on tab click

I have a bunch of tabs, in each tab I have an iframe which I want to load only when I press the tab. I don't want all the apps to be loaded once the page is loaded. How can this be accomplished?
I'm new to JSP and Javascript..
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#home">Home</a></li>
<li><a data-toggle="tab" href="#menu1">First Menu</a></li>
<li><a data-toggle="tab" href="#menu2">Second Menu</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade in active">
<iframe id="iframe1" src=""></iframe>
</div>
<div id="menu1" class="tab-pane fade">
<iframe id="iframe2" src=""></iframe>
</div>
<div id="menu2" class="tab-pane fade">
<iframe id="iframe2" src=""></iframe>
</div>
</div>
Ajax is better!
As suggested from #PDKnight you should use ajax and at the end of this solution you'll see why.
Here is a working fiddle for you (jQuery code).
jQuery(document).ready(function($) {
$('.nav-tabs > li > a').on('click', function(event) {
//avoid <a> tag to load his href
event.preventDefault();
//getting the main subjects
var id_of_selected = $(this).attr('load-in');
var link_to_load = $(this).attr('href');
var iframe_to_load = $(document).find('#' + id_of_selected);
console.log(iframe_to_load);
//make the magic
if (iframe_to_load.length) {
iframe_to_load.attr('src', link_to_load); //this fires the link to destinated iframe
iframe_to_load.parent().addClass('active'); //this make the parent div container of iframe visible
}
});
});
.tab-pane {
display: none;
}
.active {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="https://www.google.com/maps/embed?pb=!1m14!1m12!1m3!1d11173.933562701173!2d12.2363393!3d45.560717000000004!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!5e0!3m2!1sit!2sit!4v1452004351715" load-in="iframe1">WORKING!</a>
</li>
<li><a data-toggle="tab" href="http://www.youtube.com" load-in="iframe2">First Menu</a>
</li>
<li><a data-toggle="tab" href="http://www.facebook.com" load-in="iframe3">Second Menu</a>
</li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane fade in">
<iframe width="200" height="200" style="width:200px;height:200px;" id="iframe1" src=""></iframe>
</div>
<div id="menu1" class="tab-pane fade">
<iframe id="iframe2" src=""></iframe>
</div>
<div id="menu2" class="tab-pane fade">
<iframe id="iframe3" src=""></iframe>
</div>
</div>
You'll see (in console while pressing "First Menu" or "Secon Menu") that you cannot load links that doesn't allow X-ORIGIN read more here).
But you can load in the iframes just the first tab (the working one) because google maps allow X-ORIGIN embeding.
Please thumbs up if worked for u ;)

Categories

Resources