Stop any JavaScript from accessing parent.window.opener.location

Seeking a solution to avoid closing the page when clicking on an external link using JavaScript code that runs on another page.

if(parent.window.opener) parent.window.opener.location='//www.domain.com';

I have numerous links, making it challenging to remove them or use the annoying window.onbeforeunload function.

Is there any code available to achieve this?

Thank you.

Answer №1

Employ this method

    var website = function(url) {
        return url.replace('http://','').replace('https://','').split('/')[0];
    };

    return website(location.href) !== website(url);
}

Implement this approach to prevent the default action of a link:

document.getElementsByTagName('a').addEventListener("click", function(event){
if(website(event.target.href)){
event.preventDefault();
}
}, false);

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

Which is the better approach: extending properties of a Node module.exports object, or completely replacing the object?

When working in Node.js, which approach is more optimal and why? Is it better to completely replace the module.exports object like this: module.exports = { myFuncOne = function(thing) { console.log(thing); }, myFuncTwo = function(stuff) { c ...

"Encountering an issue where ng-repeat doesn't reflect updates after adding a new value to the array. Attempted to use $scope.$

Whenever the chat is opened and a name is clicked, it triggers $scope.openChat(user) which adds a name to the $scope.chat.openChats array. The ng-repeat is supposed to monitor this array for any new values but does not update. I attempted using $scope.$app ...

What steps can I take to ensure my CSS component remains unaffected by the global CSS styles?

My navbar component is not displaying the styles correctly as intended. I have a Navbar.module.css file to style it, but after using next-auth for social login, only the buttons remain unstyled while everything else gets styled. The code snippet for impor ...

Having difficulty positioning the Divider correctly among Grid items

I am trying to add a Divider between each item in my Grid. The desired output should resemble this:- Col 1 | Col 2 | Col 3 However, I am facing difficulties as the Divider is not visible and the items are displayed vertically. import "./style ...

What is the best approach to manage a file upload in Meteor?

Is there a recommended method for managing file uploads in Meteor? ...

Activate scroll function exclusively upon hovering over specific object

I am working on a page with an object that allows users to zoom in and out. I want to create a special zoom function that will be triggered when the user scrolls while their cursor is hovering over this object. If the cursor moves away from the object, th ...

The Controller Method is passing NEW HTML, but the Ajax request is returning outdated HTML

In my work with .NET Framework / MVC 4 / C#, I encountered an issue where a View containing a Partial View was not being properly updated/refreshed after user actions such as clicking a SAVE or CANCEL button. The problem stemmed from an Ajax request callin ...

Easy ways to manipulate the style of array components in Vue.js

Hey there all you programmers, I'm reaching out to see if any of you have insight on how I can modify the style of a specific component within an Object array. This is the code snippet: <template> <div> <ul> <li v-fo ...

When launching a .app file, the Node.Js subprocess fails to initiate, however, it successfully starts when executed from a Unix executable

Currently, I am developing an application using ElectronJS with a Python backend. The communication between Electron and Python is facilitated through Flask, which hosts a webserver to handle JSON data exchange. The issue I am facing is that the applicati ...

Looking to display database information using javascript

Currently, I am working on a project involving PHP code where I retrieve variables from an input and utilize AJAX. Here is the code snippet: $.ajax({ type: "GET", url: "controller/appointment/src_agenda.php", data: { function: "professional", ...

Is the typeahead feature acting unusual - could this be a glitch?

My input field uses typeahead functionality like this: <input type="text" id="unit" name="unit" class="form-control form-input" ng-model="item.unit" autocomplete="off" typeahead-min-length="0" uib-typeahead="unit a ...

Employing conditional statements to adjust the size of an image or background identifier in JavaScript

As I dive into the world of Javascript, I find myself faced with the challenge of modifying existing code using if / else statements. The original code snippet in question is as follows: document.getElementById("button1").addEventListener("click", functio ...

Avoiding code duplication in Angular: tips for optimizing functions

Is there a way to avoid repeating the same for loop for a second variable and use only one? I'm trying to apply the "Don't repeat yourself" method here. Should I consider using an array? JS: var app=angular.module('xpCalc', []); app.c ...

Modify radio button colors upon selection with a variety of colors

Looking to create Yes/No buttons that start with a default state of null and change color when pressed - green for yes, red for no. Check out this example code: <label class="formheading">Apparatus group is correct</label> <fieldset data-r ...

Trouble with Fetching Data by ID in MongoDB 2.0.0 Driver

In my NodeJS/Express web service project, I am using the MongoDB 2.0.0 driver to interact with the database. UPDATE: Following previous feedback, I have made changes to my code, including removing the db.close() call. Everything works smoothly when I per ...

development session not persisting on local server (localhost:4200)

Currently, I am utilizing angular for the frontend and node.js along with express for the backend of my application. The interesting observation is that when I run the app on localhost:3000 (the designated port for the express app), everything operates cor ...

Guide on importing npm packages without TypeScript definitions while still enabling IDE to provide intelligent code completion features

I am currently utilizing an npm package that lacks type definitions for TypeScript. Specifically, I'm working with the react-google-maps library. Following their recommended approach, I have imported the following components from the package: import ...

Altering the value of a global variable through a local function in JavaScript

I have developed a straightforward piece of Java script code. I'm looking to update the value of a global variable using a local function. Whenever I trigger the value1() function, I want the output to display as "2". Is there a way I can achieve this ...

Instructions for including a sentence in an array as a single element with the split method

I have a string and I want to turn it into an array by splitting it into individual elements. For example: let str = "add 2017-04-25 2 USD Jogurt" str.split(" "); ["add", "2017-04-25", "2", "USD", ...

Can WireMock be used to send an HTML file as a response?

Is it possible to return an HTML file as a response in WireMock and how can this be achieved? ...