Load page content dynamically with Ajax in a specific div while still allowing the option to open the content in a new tab by right-clicking

As I work on developing a new website that utilizes a MySQL database to sort various items into categories and subcategories, I have implemented a script that dynamically loads category content into a div without requiring a page reload. This seamless loading also updates the URL in the browser's address bar, ensuring that refreshing the page will display the correct category content. However, I am facing an issue with enabling the "open in new tab" functionality when right-clicking, as the content is loaded through a JavaScript/AJAX function.

In my attempt to resolve this, I tried using:

<a href="javascript: liveresults('mycategory');">

in place of:

<a onclick="liveresults('mycategory');">

Unfortunately, this approach does not trigger the "open in new tab" option in Firefox and leads to an "about:blank" page in Chrome. While holding CTRL and clicking in Firefox allows the functionality to work, it modifies the URL to "javascript:%20liveresults('mycategory');" which presents a challenge for users.

If anyone has suggestions on how to tackle this issue, such as potentially creating a custom right-click menu similar to , I would greatly appreciate any insights or guidance.

Answer №1

Consider trying out this alternative approach:

function retrieveData(event, parameter) {
    if (!event.ctrlKey) {
        console.log(parameter)
        return false
    } else
        return true
}

<a
  href='http://google.com'
  onclick='return retrieveData(event, "category")'
>Testo</a>

No further explanation needed.
Regarding the stylistic standpoint, I recommend adopting this structure instead:

function retrieveData(event) {
    if (!event.ctrlKey) {
        console.log(event.target.dataset.category)
        return false
    } else
        return true
}

<a
  href='http://google.com'
  onclick='return retrieveData(event)'
  data-category='my_category'
>Testo</a>

This may not be compatible with older versions of IE that use srcElement instead of target, but it definitely adds a touch of sophistication. However, the choice is yours.

Alternatively (if you're feeling bold =), you can eliminate repetitive link specifications:

function retrieveData(event) {
    if (!event.ctrlKey) {
        var urlPart = event.target.href.split('/')[3]
        // consider using another method for extraction, for now 'mail'
        console.log(urlPart)
        return false
    } else
        return true
}

<a
  href='http://google.com/mail/'
  onclick='return retrieveData(event)'
>Testo</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

Getting an image file to an API server using Node.js

I am looking to create an API server using node.js, and one of the requirements is to upload image files to it. While I have successfully implemented the logic for the GET method in my code, I am struggling with how to write the logic for the POST method ...

Tips for Printing a div Element with Horizontal Scrollbars

My webpage has both vertical and horizontal scroll bars, but when I use window.print(), it only prints the visible content in the window. Is there a way to print the entire scrollable content within the window? ...

What is the best way to perform an AJAX request in Typescript with JSON data?

Currently, I am delving into the realm of AJAX and encountering some hurdles when attempting to execute an AJAX request with parameters. Specifically, I am facing difficulties in sending JSON data: My approach involves utilizing Typescript in tandem with ...

The issue of Access-Control-Allow-Origin restriction arises specifically when using the gmap elevation API

My attempts to retrieve the elevation of a site using latitude and longitude have been unsuccessful due to an error I encountered while making an ajax call. Regardless of whether I use HTTP or HTTPS, I receive the following error message: "No 'Access ...

Navigate through set of Mongoose information

I have a challenge where I need to retrieve an array of data from Mongoose, iterate through the array, and add an object to my Three.js scene for each item in the array. However, when I try to render the scene in the browser, I encounter an error that say ...

Apply styles specifically to elements that contain a child element

I have a scenario where there are multiple <p> elements with the same class names, but only one of them has a child element. My objective is to highlight only the <p> that contains a child, however, my current code ends up highlighting all of t ...

Printing an array from JavaScript to an EJS template: Tips and Tricks

I'm currently working on a database-driven website that focuses on searching through people's profiles. When attempting to display information from a database query, I am encountering the issue of receiving [object Object]. Below are snippets o ...

Responsive menu is failing to respond to the click event

I'm facing an issue with my responsive menu on mobile phones. It should display two buttons - one for the navigation bar and the other to toggle the side bar by removing the classes hidden-xs and hidden-sm. However, I am having trouble getting the btn ...

The image will remain static and will not alternate between hidden and visible states

I am facing a challenge trying to toggle an image between 'hidden' and 'show' My approach is influenced by the post on How to create a hidden <img> in JavaScript? I have implemented two different buttons, one using html and the ...

Tips for verifying the "truthiness" of an object, removing any falsy values, and making changes to the object

I am faced with the task of examining each property of an object to determine if it is truthy, and then removing any that are not. var user = { name: 'my name', email: null, pwHash: 'U+Ldlngx2BYQk', birthday: undefined, username: &ap ...

Utilizing an Empty Array within an AngularJS Controller Function Invoked by a Directive

I have encountered a tricky issue that appears to be simple, but I am struggling to find a solution. Currently, I am developing a to-do list application using Angular and Angular-Material. The main part of the application is located in a file named main. ...

Stop the background from scrolling and prevent auto-jumping to the top on mobile devices

When users click on the hamburger icon in the top right of our mobile site, I want the drop-down menu to appear and be scrollable without the background scrolling. I tried using JavaScript to set the body to fixed when the menu icon is clicked, but this ca ...

Issue with Pebble SDK/SimplyJS failing to recognize the tab character

Currently, I am facing an interesting challenge while attempting to make my Pebble watch recognize the escape sequence character \t when sending data to my watch using SimplyJS. Here is the code snippet I have been working with: simply.scrollable(tr ...

Can we avoid the error callback of an AJAX request from being triggered once we have aborted the request?

Initially, I encountered a challenge where I needed to find a way to halt an AJAX request automatically if the user decided to navigate away from the page during the request. After some research, I came across this helpful solution on Stack Overflow which ...

How to create a continuous loop for a JavaScript carousel

I have a simple carousel set up with this code. I'm looking to make it loop back to the beginning after reaching the third quote-item. Can anyone provide some guidance? Thank you! This is the JavaScript used: <script> show() $(functi ...

How can I identify the currently active partial in mvc3 framework?

Is there a way to dynamically determine which partial is currently active in the DOM so that I can apply CSS to it? Also, how can I create an anchor that allows users to copy and paste a URL to a specific partial? EDIT: Assume I have three links, where t ...

Is there a way to determine the number of clicks on something?

I'm attempting to track the number of times a click event occurs. What is the best method to achieve this? There are two elements present on the page and I need to monitor clicks on both of them. The pseudo-code I have in mind looks something like ...

Eliminate list items with a keyboard stroke

I am currently developing a straightforward todo list application using JavaScript. The main functionality I am trying to implement is the ability to add new items from an input field to a list, as well as the option to remove items from the list. While ...

Issue: The function "generateActiveToken" is not recognized as a function

I encountered an issue in my Node.js project and I'm unsure about the root cause of this error. Within the config folder, there is a file named generateToken.js which contains the following code snippet: const jwt = require('jsonwebtoken'); ...

Utilize AJAX to retrieve the output of a PHP randomizer

Current situation: I have a PHP file with a randomizer function and HTML that utilizes this function to display strings from a separate text document. The Function: <?php function rand_line($fileName, $maxLineLength = 4096) { $handle = @fopen($fileN ...