Modifying an image using JavaScript and CSS

I've been experimenting with adding an Adblock detector that will show one image when detecting Adblock and another when not detecting it. The challenge lies in the fact that CSS controls the display and formatting of the image. Is there a way to manipulate the image displayed by CSS via JavaScript?

html:

<div id="adblockFrame">
<script type="text/javascript" src="js/advertisement.js"></script>
<script type="text/javascript">
    if (document.getElementById("TestAdBlock") != undefined)
    {
        document.getElementById('adblockFrame').innerHTML="<img src='images/ImageHolderno.png'      alt='no adblock' />";
    }
    else
    {
        document.getElementById('adblockFrame').innerHTML="<img src='ImageHolderyes.png' alt='Adblock detected!' />";
    }

Please note that the image placeholders are empty and do not serve any purpose since the image is defined by CSS.

CSS:

#mainPicture .picture
{
position:relative;
width:650px;
height:325px;
top:10px;
background-image:url(../images/minibg1.png);
background-repeat:no-repeat;
margin-left:10px;
}

Answer №1

JavaScript:

const adFrame = document.getElementById('adblockFrame');
if(adFrame) {
    adFrame.className = 'noAdsAllowed';
}

CSS:

.noAdsAllowed {
    background:url(AdBlockerImage.png) no-repeat;
}

Or add it dynamically using pseudo-elements:

.noAdsAllowed::after {
    content: url(AdBlockerImage.png);
}

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

The initial page in jqgrid may show rowobject value as undefined, but subsequent pages will display the correct rowObject values

While working with jqgrid-4.8, I encountered an issue where the rowObject value is coming up as undefined in the first page but values are received in subsequent pages when using rowObject.name. However, if rowObject[0] is used, the values of rowObject are ...

Locate records through an array of nested references using Moongoose (MongoDB)

Given the collection schema shown below: var terminalSchema = new mongoose.Schema({ name: 'String', ip: 'String' }); var wayPointSchema = new mongoose.Schema({ name: 'String', description: 'String' ...

Unable to make CSS footer stay at the bottom of the page following the utilization of V-for to display a list of items in

Footer seems to be misbehaving by not staying at the bottom and instead showing under the items rendered using v-for. This issue is only happening on this page while it is working fine on others. <template> <div> <!-- Item renderin ...

How to align Vimeo embed to the left side

Is there a way to align a Vimeo iFrame to the left instead of it automatically centering itself? I want more control over its alignment within my flexbox container. I've created a Flexbox container named .example-div with two internal flex containers ...

The error code 405 (Method Not Allowed) occurs in Ajax when the action field is empty or identical to the current page

Special thanks to @abc123 for sharing the code below in one of their posts: <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <form id="formoid" a ...

Using inline SVG as a background in CSS

Recently, I've been on the hunt for a way to create a blur effect on a background specifically for IE10+. After experimenting with StackBlur, I found that it did work, but unfortunately, it was quite slow due to the size of my background image. I&apos ...

Is it possible to create an HTML link that prevents users from navigating back to the previous page or displaying the previous link in their browsing history?

I've been tasked with creating a button on a client's website that links to another page, like Google, for the purpose of assisting someone seeking help in case of an emergency. The challenge is finding a way to prevent the user from easily retur ...

Ensure that you include validation in the ajax request when loading the message content from a separate file

ajax code for adding data to the database <script type="text/javascript"> $(function() { $(".submit_button").click(function() { var textcontent = $("#content").val(); var dataString = 'content='+ textcontent; if(textcontent=='') ...

Using the GUI builder in Google App Script to include list box elements

Just starting out with javaScript and could use some guidance on using App Script. I've inserted a list box called "List Stocks" in GUI Builder, alongside a function named "listStockQuotes()" in Events. However, I'm unsure of how to add elements ...

jQuery does not cache Ajax requests by default

I have implemented the following code to make an AJAX request. I am trying to determine if the requests are being cached by using the Chrome developer tools. However, when I check the request tab, I notice that all data is always being pulled from the serv ...

What is the difference in memory usage for JavaScript objects between Node.js and Chrome?

It's puzzling to me why the size of the heap is twice as large as expected. I meticulously constructed a binary tree with perfection. I suspect v8 recognizes that each node consists of 3 fields. function buildTree(depth) { if (depth === 0) return n ...

Tips on implementing a Javascript function triggered by a user's click within the video player frame

<script> function greet() { alert("hello"); } </script> <iframe width="100%" height="315" src="https://www.youtube.com/embed/AGM0ibP1MRc" onclick="greet()"></iframe> .Kindly assist me, please. ...

unable to locate the XPath for the anchor element nested within a span element

Having mastered the use of xpath, I am facing a challenge with the following link - . Can someone guide me on how to locate the xpath for the "Demo" hyperlink on this site? I attempted using //a[text()= 'Demo '] but please conside ...

Most effective method to avoid updating a node_modules package

tag: After downloading and installing a node_module (npm package)... I have customized the internal files within the node_modules folder to better fit my requirements. Although using it as a node_module is most convenient for me, I am concerned that futur ...

My Discord bot seems to be malfunctioning and failing to send out

I am new to discord.js and I'm having trouble getting my bot to respond with a message when I send one on the server. The bot shows as online in Discord and "Logged in!" appears in the console when I run it, so client.on("ready") seems to be working f ...

Issue with React Bootstrap: The selected value in the Dropdown component does not update with the

Encountering problems when trying to update the dropdown title after selecting to display the correct data on the page. Upon arriving on this page, the selected option in the dropdown is passed as a prop. However, attempting to switch to other options in ...

What is the best way to split two sets of radio buttons with the same name into distinct blocks in an HTML form?

For my project, I am working with two sets of radio buttons where the values are stored in a Database. Depending on another result in the HTML form, I need to display one set of radio buttons or the other. The issue arises when using the same name for all ...

If the FedEx function does not receive a payment, it will need to return a value of Payment Required

I am struggling with understanding functions, parameters, and arguments in JavaScript as I am new to it. My goal is to have a function that returns different values based on the payment amount. If no payment is passed, it should return "Payment Required", ...

Displaying an alert on a webpage that shows a label input using JavaScript and

I'm currently working with HTML5 and JavaScript and I'm facing a challenge. I want to create a feature where users can input any word into a label, and when they click on a button, an alert is triggered with the given text. However, despite my ...

Modifying the boolean variable value in aspx.vb code

Recently, I started learning asp.net and I am facing an issue with changing the value of my boolean variable when I click a button. Below is my code: Partial Class Title Dim checker As Boolean Protected Sub Enterbutton1(ByVal sender As Object, ByVal e ...