Create the links using javascript - javascript

I have a implemented boostrap 4 navbar, and have a function that creates links.
It works and is there any efficient method to do in javascript.
I have three function that creates link for each hyperlinks .
Is it possible to do using cookie or any other efficient method using javascript
<script>
function setAboutLink(lang){
var url = lang+"/aboutus"; // will be xyz.com/en/aboutus
window.location.href= url;
}
function setContactLink(lang){
var url = lang+"/contact"; // will be xyz.com/en/contact
window.location.href= url;
}
function setListLink(lang){
var url = lang+"/list"; // will be xyz.com/en/list
window.location.href= url;
}
</script>
<nav class="navbar navbar-expand-md navbar-light bg-light fixed-top pb-0">
<a class="navbar-brand" href="/"+selectedLanguageName><img src="/images/logo.png" alt="" class="logoC mr-4"
style="max-width: 100px"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-between" id="collapsibleNavbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="javascript:" onclick="setAboutLink(window.location.href)"
>About Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="javascript:" onclick="setContactLink(window.location.href)"
>Contact Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="javascript:" onclick="setListLink(window.location.href)"
>List</a>
</li>
</ul>
</div>
</nav>

I don't know why cookie comes into the picture, you just need to need efficient method here, You can write a generic function for that, In the whole entire website you may use it.
function redirection(slang){
window.location.href = window.location.href + '/' + slang
}
<nav class="navbar navbar-expand-md navbar-light bg-light fixed-top pb-0">
<a class="navbar-brand" href="/"+selectedLanguageName><img src="/images/logo.png" alt="" class="logoC mr-4"
style="max-width: 100px"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-between" id="collapsibleNavbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="JavaScript:Void(0)" onclick="redirection('aboutus')" >About Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="JavaScript:Void(0)" onclick="redirection('contact')"
>Contact Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="JavaScript:Void(0)" onclick="redirection('list')"
>List</a>
</li>
</ul>
</div>
</nav>

<script>
var lang = 'en'; // default is en
if (localStorage.getItem('lang') === null){
localStorage.setItem('lang', 'ch'); // tou can have any condition for lang and set it here
} else {
localStorage.getItem('lang')
}
function setLink(path){
// if lang var is there in localStorage lang var is ch otherwise en
var url = window.location.origin + '/' + lang + '/' + path
window.location.href= url;
}
</script>
<nav class="navbar navbar-expand-md navbar-light bg-light fixed-top pb-0">
<a class="navbar-brand" href="/"+selectedLanguageName><img src="/images/logo.png" alt="" class="logoC mr-4"
style="max-width: 100px"></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-between" id="collapsibleNavbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="javascript:" onclick="setAboutLink('aboutus')"
>About Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="javascript:" onclick="setLink('contactus')"
>Contact Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="javascript:" onclick="setLink('list')"
>List</a>
</li>
</ul>
</div>
</nav>
I write localStorage in first of your script. If localStorage lang var not exist, you set localStorage lang var with ch, otherwise for first time you set it for user in localStorage.
If localStorage not exist, default lang is en.

Related

Bootstrap 5.3.0 Dropdown menu not working

My dropdown is working as in desktop and in movile version and ipad plus it toggle and collapse.
The issues is when i press it it only show the button but it doenst shwo the dropdown list after it is ipad version even i added javascrip jquery and css.
This is my code:
<script type="text/javascript">
$(document).ready(function() {
$('dropdown-toggle').dropdown()
});
</script>
<nav class="navbar navbar-expand-lg bg-dark navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="">APIzoid</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data- target="#navbarToggleButton" aria-controls="navbarToggleButton" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerButton">
<ul class="navbar-nav ms-auto me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Fitness</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
<script src="jquery/jquery.js"></script>
<script type="text/javascript" src="./javascript/script.js"></script>

how to collapse navbar onclick (bootstrap5)

I am asking for a way to collapse this nav-bar after link click rather than javascript event listener or data-bs-toggle and data-bs-target both methods answered in that article (article link) are not working with my code.
here is my code:
<header class="header-nav" id="home">
<nav class="navbar navbar-expand-lg custom-nav fixed-top">
<div class="container-fluid">
<a id="brand" href="#"><span>C</span> Solutions</a>
<button
class="navbtn"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span><i class="fas fa-bars burger-menu"></i></span>
</button>
<div
class="collapse navbar-collapse justify-content-between"
id="navbarSupportedContent"
>
<ul class="navbar-nav m-auto mb-2 mb-lg-0 text-center">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#home"
>Home</a
>
</li>
<li class="nav-item">
<a class="nav-link" href="#">about us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#services">services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">why choose us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">careers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">portfolio</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#our-clients">our clients</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">contact us</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<button class="btn btn-primary">request a quote</button>
</ul>
</div>
</div>
</nav>
</header>
can any one help please?
Your code works, you only forgot to add the boostrap.bundle.min.js in your index.html.
Add this in your index.html. that's all!
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
And you don't need to add extra js code. Only add this in your index.html and it should work.

How can I create a reusable navbar across multiple HTML pages with Bootstrap 5?

I would prefer a strictly JavaScript way of doing this if possible. Not really wanting to introduce a JavaScript framework into my project.
Layout:
./index.html
./about.html
./crew.html
./flights.html
./events.html
I want every page to have the same navbar such that I don't have to repeat myself and fix every page every time the order of the links change.
Here's my example navbar:
<nav class="navbar navbar-expand-xl navbar-light">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#toggleMobileMenu" aria-controls="toggleMobileMenu" aria-expanded="true" aria-label="Toggle Navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse show" id="toggleMobileMenu">
<ul class="navbar-nav mx-auto">
<li class="nav-item text-center">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item text-center">
<a class="nav-link" href="about.html">About</a>
</li>
<li class="nav-item text-center">
<a class="nav-link" href="crew.html">Crew</a>
</li>
<li class="nav-item text-center">
<a class="nav-link" href="flights.html">Flights</a>
</li>
<li class="nav-item text-center">
<a class="nav-link" href="events.html">Events</a>
</li>
</ul>
</div>
</nav>
Depending on what kind of browser support you need you could use a template literal (back ticks) to define your navigation. Then use insertAdjacentHTML to place it where you need it on all your html pages. If your page is fairly simple you could just insert the nav after begin on the body tag.
const navigation = `
<nav class="navbar navbar-expand-xl navbar-light">
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#toggleMobileMenu"
aria-controls="toggleMobileMenu"
aria-expanded="true"
aria-label="Toggle Navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse show" id="toggleMobileMenu">
<ul class="navbar-nav mx-auto">
<li class="nav-item text-center">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item text-center">
<a class="nav-link" href="about.html">About</a>
</li>
<li class="nav-item text-center">
<a class="nav-link" href="crew.html">Crew</a>
</li>
<li class="nav-item text-center">
<a class="nav-link" href="flights.html">Flights</a>
</li>
<li class="nav-item text-center">
<a class="nav-link" href="events.html">Events</a>
</li>
</ul>
</div>
</nav>
`
document.getElementById("nav-container").insertAdjacentHTML('afterbegin', navigation);
<div id="nav-container"></div>

Change background color with JQuery problem

I have created boxes of different colors. When the user clicks on one of the box, the background color changes.
I found a code to do this and everything works fine.
But now the menu links no longer work.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="color-boxes">
<div data-color="#000000" class="Black"></div>
<div data-color="#7E392F" class="BurntHenna"></div>
<div data-color="#AE856C" class="TawnyBirch"></div>
<div data-color="#DAB58F" class="Sheepskin"></div>
</div>
<script>
$(document).ready(function(){
$("div").click(function(){
var Color = $(this).attr("data-color");
$("body").css("background", Color);
return false;
});
});
</script>
my navbar:
<nav class="navbar navbar-expand-lg navbar-dark">
<a class="navbar-brand" href="">
<img src="media/logo-bressana.png" width="250px" alt="">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="bio.html">Bio</a>
</li>
<li class="nav-item">
<a class="nav-link" href="press.html">Press</a>
</li>
</ul>
</div>
</nav>

Why won't scrollspy work in Bootstrap 4 if i use ./ in the href?

If I remove ./ from the href attribute then it works fine. Why?
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top navbar-toggleable-md" id="navbar1">
<div class="container-fluid">
<a class="navbar-brand" href="#top">
<img src="./img/logo.png" alt="" class="img-fluid logo">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item ">
<a class="nav-link" href="./#features">Features <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./#services">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./#video-play">Showreel</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./#gallery">Gallery</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./#team">Team</a>
</li>
<li class="nav-item">
<a class="nav-link" href=./ "#news">News</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./#contact">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Shop</a>
</li>
<li class="nav-item apply-now text-white">
<a class="nav-link text-white" href="./form-fee-easy.html">Apply Now</a>
</li>
</ul>
</div>
</div>
$('body').scrollspy({
target: '#navbar1'
});

Categories