How to create a hyperlink that only functions when JavaScript is turned off?

Exploring the realms of HTML 5, jQuery, CSS3, and the concept of manipulating elements based on JavaScript's state (<noscript> tags).

In my quest for knowledge, I have two burning questions:

  1. How can I ensure a hyperlink functions ONLY when JavaScript is DISABLED?
  2. If JavaScript is enabled, how do I make sure that my drop-down login menu shows INSTEAD of redirecting through the hyperlink?

HTML

A simple approach involves hiding the cart area and modifying the login link depending on JavaScript's status.

<!-- In case JavaScript is disabled, the login link directs users to a login page rather than displaying a drop-down box. -->
<noscript>
    <style>
        .login-form, .js-enabled{
            display: none;
        }
        .cart{
            top: -80px;
        }
    </style>                                
    <div class="cart">
        <a href="#" id="cart_img">
            <img src="img/bag.jpg" alt="Check Cart"
            onmouseover="this.src='img/bag-gold.jpg'"
            onmouseout="this.src='img/bag.jpg'">
        </a>
        <a href="#">Why HorseTack?</a> | 
        <a href="#">About</a> | 
        <a href="#">Contact</a> | 
        <a href="http://www.LOGINPAGELINK.com">Login</a>
    </div>
</noscript>
<!-- End JavaScript text -->

<div class="cart js-enabled">
    <a href="#" id="cart_img">
        <img src="img/bag.jpg" alt="Check Cart"
        onmouseover="this.src='img/bag-gold.jpg'"
        onmouseout="this.src='img/bag.jpg'">
    </a>
    <a href="#">Why HorseTack?</a> | 
    <a href="#">About</a> | 
    <a href="#">Contact</a> |                           
    <a href="#" class="login-box">Login</a>
        <div class="login-form">
            <form class="login-frm">
                <label><input type="text" placeholder="Username"></label>
                <label><input type="password" placeholder="Password"></label><br>
                <input type="submit" value="Login"><br>
                <a href="#">New User?</a>
            </form>
        </div>
</div>

JQuery

To prevent sudden redirects to a login page, utilizing a sliding login-box may be an elegant solution.

    // Login Box Pop-Up
jQuery(document).ready(function() {
    jQuery(".login-form").hide();
    //toggle the componenet with class msg_body
    jQuery(".login-box").click(function()
    {
    jQuery(this).next(".login-form").slideToggle(500);
    });
});

Taking steps further than Stack Overflow to indicate if JavaScript is disabled, my ultimate goal is for the hyperlink to strictly function without JavaScript involvement.

If any aspects seem unclear, or you require more details regarding my query, feel free to ask!

Answer №1

Is there a way to create a hyperlink that only functions when JavaScript is turned off? Furthermore, how can I ensure that my drop-down login menu appears instead of redirecting when JavaScript is active?

Create a JavaScript function that triggers the display of the menu. Make sure it takes the first argument (the event object).

Attach the function as an event handler to the link.

Use preventDefault() on the event object.

function (event) { /* show menu and then ... */ event.preventDefault(); }

<noscript> tags

These are usually not the optimal solution for an issue. Opt for a progressive and unobtrusive approach instead.

Answer №2

Display the hyperlink and opt to conceal it if JavaScript functionality is enabled.

Answer №3

To integrate the link into a web page, consider nesting it inside a div element. Then, if JavaScript is active, remove the actual link and assign an event to the div itself. Here's an example:

<div class="link-container">
    <a href="page.html">Link</a>
</div>

Using jQuery:

$(document).ready()
{
    $('.link-container').text('').click(function(){
        displayLogin();  // Assuming that displayLogin() contains the necessary code to show the login form
    });
}

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

Can a fixed form of `instanceof` be used?

Is there a static equivalent of instanceof? For example, instead of using: obj1 instanceof Type is there something like: TypeA instanceof TypeB? I searched for information on this topic but couldn't find anything, so I came up with the following solu ...

Customize Your ERPNext Website: Displaying a specific type of Blog Posts

Is there a way to display only a specific category of Blog Posts in a Webpage Section created using the Page Builder? I have designed a Web Template in EPRNext that showcases Blog posts on any webpage by adding a Section through the Page Builder. It' ...

How can I get the class name of the drop destination with react-dnd?

Imagine having this component that serves as a drop target module. import { useDrop } from 'react-dnd'; import './css/DraggableGameSlot.css'; type DraggableGameSlotProps = { className: string, text: string } function Draggable ...

FirebaseError: The type 'Hc' was expected, but instead, a custom Yc object was provided

I've encountered an issue while attempting to perform a batch entry. The error I'm facing involves passing an array in a .doc file. Interestingly, this approach seems to work perfectly fine on another function where I pass an array into a .doc us ...

Looking to implement a CSS effect that will hide content without removing the space it occupies on the page?

Is there a way to hide specific buttons within the right column of a 3-column table display? I am currently utilizing jQuery to achieve this function. The purpose behind hiding these buttons is to prevent any quick clicks that might interfere with the fa ...

Is there a way to utilize jQuery to navigate through multiple DIVs within a slide bar?

As a beginner in jQuery, I am facing the challenge of placing more than one div in a single slide bar. Specifically, I am developing an auction site and I need a DIV that can display multiple items within the same div, accompanied by "Next" and "Previous" ...

Do the items appear on screen only once you start typing in AngularJS?

Most things are working well except for a couple of issues Code var app = angular.module("MyApp", []); app.filter('offset', function() { return function(input, start) { start = parseInt(start, 10); return input.slice(start); }; } ...

What is the best way to upload a canvas image from a GUI to the back-end using an AJAX call and setting the content-type as "image/jpeg"?

What is the best method for saving a canvas image from GUI to back-end using an AJAX call that accepts content type "image/jpeg" and avoids the error "jquery.js: 8453 Uncaught TypeError: Illegal invocation?" HTML <canvas id="myImage"></canvas> ...

Creating vertically aligned React Bootstrap cards with responsive design

Looking for help with my card component: <Card className="m-5 border-0 shadow" style={styles.card}> <Row> <Col> <Card.Img src={props.image} style={styles.cardImage}/> </Col> <Col&g ...

ASP.Net MVC 3 incorporating the powerful features of JQGrid

Upon researching the JQGrid control, I made the decision to incorporate it into one of my ASP.Net MVC 3 Web applications. To start off, I followed Phil Haack's tutorial which was very helpful. I then attempted to implement a similar setup in my appl ...

Switching from 'click' to 'hover' will make the dropdown menu more accessible for keyboard users

I'm currently updating an existing dropdown menu that meets WCAG standards but I need to tweak the functionality so that it activates on hover instead of click. Below is the code snippet: $(document).ready(function() { $('.has-submenu'). ...

What is the best way to define a constant within a map function?

I have a VueJs computed function called linhas() that populates the rows of my DataTable. I am struggling with opening up this function to assign a value to "this.grupo", so that I can avoid using it the same way as I do with "this.maquinas". As a beginner ...

Leverage the power of PHP and cURL to retrieve JSON data from a file containing a mix of text, HTML

When utilizing PHP and cURL on THIS website, the returned file contains data that looks like this: <!DOCTYPE html> <html> <head></head> <body> <script> window['flyerData'] = { ...

Having trouble rendering arrays and objects in Node, Express, and Jade?

//What is currently being passed to the Jade template exports.displayList = function(req, res){ res.render('report', {title: 'Custom Reports', rpts:[{uri:'/reports/allocation', title:'Allocation Report' ...

Understanding the scope of variables in JavaScript when using an anonymous function with addEventListener

Upon clicking on each div, an alert will display '1' if div 1 was clicked, or '5' if div 2 was clicked. This code has been simplified for ease of use in a larger application. <html> <head> <style type="text/css"> #div ...

The button transforms once the user submits the form

I have two buttons labeled "Save" and "Saving..." <div ng-switch on="isLoading"> <div ng-switch-when="true"> <button type="button" class="btn btn-primary btn-block disabled">Saving ...</button> </div> <div ng-switch- ...

I'm currently working with React and experiencing issues with my components being unrendered

I'm currently navigating a React tutorial and I'm having issues getting this code to display some buttons. I'm utilizing codepen.io to work on the code and can provide the tutorial link if needed. const clips = [ { keyCode: 81, key ...

es-lint is issuing a warning about the presence of multiple modules within the node_modules directory that have names differing only in their casing

After reviewing all my import statements, I found that everything looks correct. The only unusual import I have is react-bootstrap, which I import as: import { Jumbotron, Button } from 'react-bootstrap'; I'm using the Jumbotron and Button ...

Error encountered when applying filter function in AngularJS/JavaScript due to syntax issue

Encountering an issue with the filter method in AngularJS/Javascript. Below is the code snippet: var temp = []; $scope.mulImage = $scope.mulImage.filter((x, i) => { if(temp.indexOf(x.filename) < 0) { temp.push(x.filename); retur ...

Shrinking my content on mobile devices with the use of "<p>" tag

I've noticed my footer, header, and forms shrink on mobile devices, but the text on my privacy pages remains the same size. Is there an easy way to make this text shrink as well? Thank you .content { border: 1px solid transparent; margi ...