javascript href clears Internet Explorer webpage

I noticed a strange issue with my HTML page. In Internet Explorer, when I click on the link, it displays the return value on a blank page. However, in Chrome, it simply executes the function without affecting the page appearance. Is there a way to make Internet Explorer behave like Chrome in this scenario?

<html>
  <body>
    <script type="text/javascript">
      function foo(){
        return true;
      }
    </script>
    <a title="call foo" href="javascript:foo()">return true</a>
  </body>
</html>

Answer №1

When it comes to the javascript: scheme URL, its purpose is to execute code that generates data, leading the browser to navigate accordingly.

If your intention is simply to trigger some JavaScript upon clicking something, avoid using a link.

Always choose the appropriate tool for the task at hand.

function foo(){
  return true;
}

document.querySelector("button").addEventListener("click", foo);
<button title="call foo">return true</a>

Answer №2

<html>
<body>
<script type="text/javascript">
function bar(){
  event.preventDefault();
  return false;
}
</script>
<a title="execute bar" href="javascript:bar()">return false</a>
</body>
</html>

Answer №3

To prevent the default event of a hyperlink in a click event, you can utilize the event.preventDefault() method. Here's an example code snippet:

<script type="text/javascript">
    function bar(e) {
        event.preventDefault();
        ////get the href attribute.
        //var link = document.getElementById("link").getAttribute("href");
        ////display the page. You could also use "window.open(link)" to open a new tab for displaying the page.
        //window.location = link;
        return true;
    }
</script>
<a title="call bar" id="link" href="https://www.example.com/" onclick="bar(this);">return true</a>

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

Include an external JavaScript file within a component to display pictures in a web browser using Angular 2

I am developing a website using Angular 2 and need to incorporate a JavaScript file into a component. The goal is for this script to adjust the height of certain images to match the height of the browser window. What is the best way to integrate this scri ...

Tips for selecting a checkbox with Puppeteer

I've implemented the code in this way: await page.$eval('input[name=name_check]', check => { check.checked = true; }); This code is intended for multiple checkboxes. However, I need it to work for a single checkbox only. Is there a way ...

Updating AngularJS: Removing the hashtag using history.pushState()

Struggling to enhance the aesthetics of the URLs in my AngularJS application, I am facing some challenges. While I can access the "/" route without any issues, the "/about" route seems to be out of reach. Please note that the project is situated at (loc ...

Guide on creating a Jasmine test for a printer utility

Currently, I am working on writing a Jasmine test for the print function shown below: printContent( contentName: string ) { this._console.Information( `${this.codeName}.printContent: ${contentName}`) let printContents = document.getElementById( c ...

Tips for extracting a website's dynamic HTML content after AJAX modifications using Perl and Selenium

When dealing with websites that utilize Ajax or javascript to display data, I'm facing a challenge in saving this data using WWW::Selenium. Although my code successfully navigates through the webpage and interacts with the elements, I've encounte ...

Can I receive a PHP echo/response in Ajax without using the post method in Ajax?

Is it feasible to use Ajax for posting a form containing text and images through an HTML form, and receiving the response back via Ajax? Steps 1.) Include the form in HTML with a submit button. 2.) Submit the form to PHP, where it will process and upload ...

Merge two JSON objects together by matching their keys and maintaining the original values

After having two separate JSON objects representing data, I found myself in the position of needing to merge them into one combined result. The initial objects were as follows: passObject = { 'Topic One' : 4, 'Topic Two' : 10, &apos ...

Issues with splicing an Angular array using pure JavaScript

Could someone please help me figure out what I'm doing incorrectly? I have some JSON data that I am converting into an array for use in an ng-repeat. Before looping through the array, I am checking the event dates against today's date and removin ...

Problem with ngStyle: Error indicating that the expected '}' is missing

I encountered an error in the Chrome console when trying to interpret the code below: <div [ngStyle]="{'width': '100%'; 'height': '100%'; 'background-size': 'cover'; 'background-repeat&ap ...

Encountering an issue with HTMLInputElement when trying to input an integer value into a label using JavaScript

script code intext = document.getElementById("intext"); totalin = document.getElementById("totalin"); function myFunction() { var x = document.getElementById("totalin"); x.innerHTML = intext.toString(); } The snippet above shows a JavaScript functio ...

Tips on placing a button at the bottom of the AppBar and keeping it fully responsive

I am attempting to place my login and log out buttons at the end of the AppBar. When I try forcing it with marginLeft: '70%',, it works but only on large resolutions. On smaller resolutions, the buttons go out of place. Here is an example of forc ...

In vue, pagination links are displayed as dots instead of numbers

Struggling to integrate pagination in vue. Facing an issue where the pagination links are appearing as dots, depicted in the attached image. The correct number of pagination links display and I can navigate through different pages using the side navigation ...

Tips for expanding third-party classes in a versatile manner using Typescript

tl;dr: Seeking a better way to extend 3rd-party lib's class in JavaScript. Imagine having a library that defines a basic entity called Animal: class Animal { type: string; } Now, you want to create specific instances like a dog and a cat: const ...

What is the best method for integrating UL / LI into JSON to HTML conversion?

Currently, I am working on converting a JSON string into HTML format. If you want to take a look at the code, here is the jsfiddle link: http://jsfiddle.net/2VwKb/4/ The specific modifications I need to make involve adding a li element around the model da ...

Discovering the properties of a class/object in a module with Node.js

Recently I delved into the world of node.js and found myself puzzled about how to discover the attributes, such as fields or properties, of a class or object from a module like url or http. Browsing through the official documentation, I noticed that it on ...

Refreshing a page will disable dark mode

I'm in the process of implementing a dark mode for my website using the code below. However, I've encountered an issue where the dark mode resets when refreshing the page or navigating to a new page. I've heard about a feature called localst ...

The component in React does not refresh after updating the state

I have a React page where I am displaying a list of quizzes fetched from an external API. There's also a "New Quiz" button that opens a dialog with a form for users to create a new quiz. My issue is with making the table re-render once the POST reque ...

What is the solution to the error "Maximum update depth exceeded. Calls to setState inside componentWillUpdate or componentDidUpdate" in React?

Everything was running smoothly until this unexpected issue appeared. I attempted to change the condition to componentDidMount, but unfortunately, that didn't resolve the problem. The error is occurring in this particular function. componentDidUpd ...

"Exploring the world of jest.doMock: A guide to mocking

Here is the code snippet I am testing: ... import data from '../data/mock.json'; // function is async export const something = async () => { try { ... if (!data) { throw 'error is here!'; } ...

Please optimize this method to decrease its Cognitive Complexity from 21 to the maximum allowed limit of 15. What are some strategies for refactoring and simplifying the

How can I simplify this code to reduce its complexity? Sonarqube is flagging the error---> Refactor this method to reduce its Cognitive Complexity from 21 to the allowed 15. this.deviceDetails = this.data && {...this.data.deviceInfo} || {}; if (th ...