What is the best way to add a CSS link if a request is not made using AJAX?

My webpage, 'foo.html', retrieves data from a database using AJAX ('ajax.html?options=option1') to populate a table. The table is styled nicely with CSS that is linked to 'foo.html'. However, I also want the ajax.html page to be styled when accessed directly. When I add

<link rel="stylesheet" type="text/css" href="/dev/css/default.css" />
to ajax.html, it inserts the link into foo.html as well, which is not desired. Is there a way to prevent the CSS link code from appearing in the AJAX call or only display on non-AJAX calls?

Any advice would be appreciated.

Answer №1

One solution that comes to mind is to include an extra parameter that specifies the context in which the function is called.

Answer №2

To simplify this task, utilizing jQuery is highly recommended.

Begin by loading the ajax.html page using jQuery.get(). Upon success, execute the following steps:

  • Remove the existing stylesheet:
    $('link[rel=stylesheet]').remove()
    ;

If you wish to add a new stylesheet at a later point:

var link = $("<link>");
link.attr({
        type: 'text/css',
        rel: 'stylesheet',
        href: 'http://example.com/stylesheet.css'
});
$("head").append( link );

Alternatively, if you need to modify the stylesheet URL later on:

$("link").attr("href","http://example.com/stylesheet.css");

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

Customizing arrays in DataTables allows for creating new columns and displaying them in the table according to the specified customizations

Displaying dynamic data in a data table involves taking an array as input and customizing the fields within that array. ...

Implementing uniform column width in a Material-UI Grid

Looking for a way to create a single column layout with items of equal width using the Material-UI Grid system? Check out this sample code: <Grid container align="center" direction="column"> <Grid item className={classes.item}> < ...

Is there a way to have the headline gently fade in and subtly rise when the page loads using CSS?

Is it possible to make the headline fade in and move up slightly on the home page using CSS after the user lands on the website? For a visual reference, check out this example on . I know it can be achieved with translateY, but I have never tried it before ...

Dynamically insert innerHTML content into table rows using JavaScript in PHP is a fantastic way to customize

Having trouble with innerHTML in this scenario, looking to achieve something along these lines: <table width="100%" border="0" id="table2"> <?php include("connection.php"); $sql=mysql_query("select* from b1"); while($res=mys ...

What could be causing the server to not successfully receive the ajax request?

I need to conduct integration tests on a website that routes all requests through a proxy: var express = require("express"), http = require("http"), port = (process.env.PORT || 8001), server = module.exports = express(), httpProxy = requir ...

Issue with using the character '#' in a JSON as a parameter in $AJAX URL

I have encountered an issue with the following JQuery ajax call where a JSON parameter is being truncated on the server if LoadFileSeperator = '#'. Despite this, the code works fine otherwise. What can I do to encode LoadFileSeperator in a way t ...

The functionality provided by Ajax and React

I have a form that uses AJAX to submit data successfully. However, I am looking to pass the data from this AJAX call to a React component in order to update the view once the data has been submitted. How can I achieve this by passing data to React using a ...

Unable to trigger JavaScript function from ASP.NET MVC HTML view

I am encountering an issue with my ASP.NET MVC project where I am attempting to call a JavaScript function to record a user's selection from a listbox. Despite duplicating the JavaScript function in multiple places, it is still not being found. What c ...

Ajax request terminates once PHP has looped for a specific duration of time (2 minutes)

My challenge involves a button that is responsible for checking over 300 posts for a specific value and other conditions using about 20 if-else statements. Strangely, the ajax call initiated by this button halts after completing around 73 loops within a sp ...

Transmit JSON Data via POST Method and Retrieve Using Node.js (Without the Use of Body Parser)

Can anyone help me with sending JSON data from HTML and receiving it in Node.js? I've been trying for hours and I'm getting frustrated. It seems like I'm close to a solution but no luck so far. In my VSCODE, the response is null (req.body = ...

Converting non-null values in a JSON object to null within an MVC Controller method

Take a look at the image below... The information displayed in the upper section shows the JSON data from my client that is being sent to the MVC Controller. I directly copied this from Chrome Dev Tools. In the lower part, you can see the actual data rec ...

The 'data-intro' property cannot be bound to the button element as it is not recognized as a valid property

I've been using the intro.js library in Angular 8 and so far everything has been working smoothly. However, I've hit a roadblock on this particular step. I'm struggling to bind a value in the data-intro attribute of this button tag. The text ...

Arrange the DIVs in the identical spot and switch them back and forth

I'm struggling with a dilemma involving two DIVs. I am looking to have two DIVs positioned in the exact same spot on the page and toggle between them. When one div disappears, the other should appear using jQuery toggle(). The challenge lies in havi ...

Upon selecting a dropdown menu option, populate a textbox with information retrieved from the database

I'm encountering an issue with my code. I'm attempting to populate a textbox with data fetched from the database. The goal is to display the price of the selected item from my dropdownmenu. However, it's not functioning as expected. While I ...

Refresh data in bootstrap table using a JSON dataset for a seamless user experience

Working on a website project with Bootstrap, I successfully implemented a bootstrap-table that initially loads data from PHP code. Everything was functioning smoothly without any issues. However, I encountered a problem when trying to populate the bootstr ...

How to properly set margins for button components in Material UI

I'm working with a centered Toolbar in Material UI that consists of 3 components, each being a button. My goal is to add some margin around each button. I attempted to use the {mt} option within the component button, but didn't see any changes re ...

Designing Tables for User Interfaces

In a table with 4 rows, the first row must always be filled by the user. The remaining rows are optional. What is the best way to visually indicate this requirement? This should be achieved using only HTML, CSS, and jQuery. ...

HTML underlay with interactive z-index functionality

Is it possible to create an interactive Google Map with a frame image displayed? <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <input type="button" onclick="$('#frame_image').show();" val ...

Utilizing FormHelper in CakePHP to send search query to controller parameter

How can I pass a form directly to a controller parameter in CakePHP 2.4? The controller expects the parameter in the format /Model/index/by:keyword, but FormHelper is adding a question mark and an equals sign into the URL: Model/index?by%3A=keyword. Here ...

Removing dropdown lists from input forms can be achieved without using jQuery by utilizing vanilla JavaScript

Looking to build a custom HTML/JavaScript terminal or shell. Interested in utilizing an input box and form to interact with JavaScript functions/commands. Unsure of how to eliminate the drop-down menu that appears after previous inputs. Pictured terminal: ...