Click on the navlink to open the HTML page in the same window

I have developed a navigation bar with a menu that includes options like home, about-us, and more. When I click on the navbar, the nav-menu slides down to display a list of navlinks. However, when I select a navlink, such as about-us, the nav menu slides back up without opening the about-us page.

Here is the HTML code:

<div class="navbar">menu<span><img src="images/menu-arrow.png"></span></div>
<div class="nav-menu">
    <ul>
        <li><a href="index.html" id="home" class="nav-link"><h1>Home</h1></a></li>
        <li><a href="about-us.html" id="home" class="nav-link"><h1>About us</h1></a></li>
        <li><a href="services.html" id="services" class="nav-link"><h1>Services</h1></a></li>
    </ul>
</div>

And here is the Jquery code:

$(function(){
    $(".nav-menu li a").click(function(e){
        e.preventDefault(); //Prevents default anchor tag behavior
        $('.navbar span img').removeClass('flipped').css({"transition":".5s"});
        $('.nav-menu').animate({'height':'toggle'});
        var url = this.href;
        $(".main-container").load(url);
    });
});

My goal is to make the nav-menu slide up when clicking on navlinks, fade out the current page, and simultaneously show the targeted page.

Answer №1

$(function(){
    $(".nav-menu li a").click(function(e){
        e.preventDefault(); //To prevent the default anchor tag behaviour
        $('.navbar span img').removeClass('flipped').css({"transition":".5s"});
        $('.nav-menu').animate({'height':'toggle'});
        var url = this.href;
        window.open(
          url,
          '_blank' // <- This is what makes it open in a new window.
        );
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="navbar">menu<span><img src="images/menu-arrow.png"></span></div>
<div class="nav-menu">
    <ul>
        <li><a href="index.html" id="home" class="nav-link"><h1>Home</h1></a></li>
        <li><a href="about-us.html" id="home" class="nav-link"><h1>About us</h1></a></li>
        <li><a href="services.html" id="services" class="nav-link"><h1>Services</h1></a></li>
    </ul>
</div>

I have created a demonstration based on your code provided here. While you can't actually run this in the fiddle environment, the following snippet will open a new tab for you:

window.open(
      url,
      '_blank' // <- This is what makes it open in a new window.
    );

This functionality will help in opening links in a separate tab.

Answer №2

It appears that in order to achieve the desired outcome, you simply need to include the target attribute in your menu links:-

<div class="navbar">menu<span><img src="images/menu-arrow.png"></span></div>
<div class="nav-menu">
    <ul>
        <li><a href="index.html" target="_self" id="home" class="nav-link"><h1>Home</h1></a></li>
        <li><a href="about-us.html" target="_self" id="home" class="nav-link"><h1>About us</h1></a></li>
        <li><a href="services.html" target="_self" id="services" class="nav-link"><h1>Services</h1></a></li>
    </ul>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be the reason behind the malfunction of the sideNav function in bootstrap?

I am trying to implement a Bootstrap navbar, but it's not displaying correctly. I keep getting an error in my console that says: https://i.sstatic.net/UwbAS.png I've rearranged the order of my scripts in the head section, but I still can't ...

What is the best way to invert the positioning of the li elements to move upwards?

https://i.stack.imgur.com/mZaoS.png Seeking assistance on adjusting the height of bars to start from the bottom and go upwards instead of starting from the top position and going downwards. The JavaScript code below is used to generate the li elements and ...

Centering Vertical Tabs Using HTML and CSS

I followed a vertical tab tutorial on W3Schools and would like to implement it on my website. However, I am having trouble centering the tabs after reducing their width sizes. I have attempted adding div tags to the code and aligning them to the center wit ...

Transmit a PHP reply to an AJAX request

Here's the conclusion of my PHP script: if ($success) { $result = array('success' => true); } else { $result = array('success' => false, 'message' => 'Something happened'); header($_SERVER ...

Only a handful of pictures all aligned in a row

How can I style two images to be on the same line? <div> <img src=""/> <img src=""/> </div> While this code gives me images on the same line, there are spaces between them. Using inline display did not solve the issue. Any suggest ...

Verify if a request attribute has been established using jQuery

How can I determine if an attribute is present in a request object? I have specific error scenarios where an error message needs to be sent to the client side: Here is the servlet code snippet: request.setAttribute("error", "The following term was not fo ...

Incorporating float and clear techniques with the <aside> HTML element

Attempting to position two elements to the left and right of another element using floats, but encountering unexpected results. Check out the jsfiddle link provided below... https://jsfiddle.net/vx7tjbkf/ aside { margin: 0; padding: 0; width: ...

Transmit data using both jQuery and JSON technology

I'm struggling to figure out how to send a JSON structure with a file using AJAX. Whenever I try to include an image in the structure, I encounter an error. var professionalCardNumber = $("#professional_card_number_input").val(); var professi ...

Selector using Multiple Quotation Marks in CSS

Struggling to find a unique identifier for a password field? I'm experimenting with attributes and quotations, trying to figure out how to handle multiple sets. Is it necessary to mix single and double quotes when dealing with three sets? (Could a di ...

Show concealed content for individuals who do not have javascript enabled

One of the challenges I faced was creating a form with a hidden div section that only appears when a specific element is selected from a list. To achieve this, I utilized CSS to set the display property of the div to 'none' and then used jQuery t ...

What could be causing my backend to malfunction while the website is live?

Currently, I am working on a PHP5 dynamic site where content such as galleries and news can be added from the backend. Everything was functioning perfectly fine on my localhost, but when I uploaded it online, the dynamic part stopped working. The default P ...

Error message in console: AngularJS throws an error saying 'No module found: uiCropper'

Hey there, I'm looking to add an image cropper to my webpage. I came across some code on Codepen, but when I tried using it, the code didn't work and I encountered this error https://i.sstatic.net/h5F4T.png angular.module('app', [&ap ...

Generated serve IDs are having issues when trying to use tabs

I am encountering an issue with my jQuery file jQuery(document).ready(function() { var orderId= <%= OrderLi.ClientID %>; jQuery("#ApprovalTab").css("display", "block"); jQuery("#ApprovalLi").css("background-color", "#5EA8DE" ...

Adjusting Div Height According to Width using Jquery

In the case of having a class with a width set to 20%, how can I use jQuery to dynamically change the height of the class to match the same width but converted to pixels? ...

What could be causing my CSS code to not function properly within Vue.js?

Having trouble with css in VueJs. I can't seem to figure out what I'm doing wrong. Generally, my scoped style css works fine in vuejs, I can target ids and classes without any issues. The problem arises when trying to change the internal css va ...

Disable a href link using Jquery after it has been clicked, then re-enable it after a

There are several <a target="_blank"> links on my website that trigger API calls. I want to prevent users from double clicking on these buttons. This is what I have tried: .disable { pointer-events: none; } $(document).ready(function(e) { ...

Error: Unable to locate sportsRecord due to missing function

updated answer1: Greetings, I have updated the question with some detailed records and console logs for better understanding. sportsRecord = { playerTigers:[ {TigerNo: 237, TigerName: "Bird Bay Area", TigerkGroupNo: 1, isDefault: true ...

Order of tabs, dialog, and forms customization using Jquery UI

I'm currently working on a jquery dialog that contains a form split into multiple tabs. In order to enhance keyboard navigation, I am looking for a way to make the tab key move from the last element of one tab to the first element of the next tab. Cur ...

Pressing the cancel button triggers a refresh of the page within the iframe

I currently have an iframe embedded in my website, and within it is the following jQuery code: $(document).ready(function() { $('#print_button').click(function() { window.print(); }); }); The issue at hand is that when I click ...

`datatable filter function not functioning properly with alphabet letters`

Currently, I am utilizing Datatables to display a pricelist and filtering the prices by letter. However, I am encountering an issue where clicking on the letter should pass it to the processing file (bronze.fnReloadAjax), but it is not happening as expec ...