What is the proper way to retrieve this stylesheet and add it to the header of my HTML file?

I want to utilize a style sheet from Wikipedia. However, when I fetch the style sheet and attempt to pass the URL retrieved through AJAX to the head of my HTML document, the URL behaves unexpectedly.

Initially, I tried to use the fetched URL directly:

var stylesheetElem = doc.querySelector('head link[rel="stylesheet"]');      

Below is the complete code :

<!DOCTYPE html>

<!-- This file is for testing purposes, used to try and display a correctly formatted Wikipedia page -->

<html>
<head>  
    <meta charset="utf-8"/>
    <title>Game Setup</title> <!-- Tab Title -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script>
</head>

<body style="background-color:white;">

<div class='container'>
    <h1 id="title">MiniWiki</h1>
    <div id="content"></div>
</div>

<script>   
function loadPage() {
    "use strict";
    var url, doc;
    console.log("IN LOADPAGE")
    url = 'https://en.wikipedia.org:443/api/rest_v1/page/html/' + 'Ancient_Egypt';
    // Fetching the article data
    return $.ajax(url).then(function (data) {
        doc = (new DOMParser()).parseFromString(data, 'text/html');
        // Using mediawiki content stylesheet
        var stylesheetElem = doc.querySelector('head link[rel="stylesheet"]');      
        console.log("SHOW stylesheetElem");
        console.log(stylesheetElem);     
        $('head').append(stylesheetElem);
        
        // Update Content
        var contentElem = document.getElementById('content');
        var $content = $(contentElem).empty();
        Array.from(doc.body.attributes).forEach(function (attr) {
            $content.attr(attr.name, attr.value);
        });
        $content.append(Array.from(doc.body.children));
    });
}
   
loadPage();
</script>

In this instance, the fetched URL is:

<link rel="stylesheet" href="/w/load.php?lang=en&modulening.con...%7Cext.cite.styles&only=styles&skin=vector">

I expected it to include https://en.wikipedia.org/ at the start of the URL like this :

<link rel="stylesheet" href="https://en.wikipedia.org/w/load.php?lang=en&modulening.con...%7Cext.cite.styles&only=styles&skin=vector">

As it didn't, I thought I could add it myself by simply inserting this line of code just before the line

console.log("SHOW stylesheetElem");

stylesheetElem.href = "http://en.wikipedia.org" + stylesheetElem.href

When outputting the stylesheetElem URL, it unexpectedly returns the following URL:

http://en.wikipedia.orgfile//en.wikipedia.org/w/load.php?...kin=vector

What occurred here? Why didn't I receive the correct URL?

http://en.wikipedia.org/w/load.php?...kin=vector

Answer №1

If you spot dots (...) in the URL, it indicates that certain parts have been omitted by the developer tools. Rather than using this altered version, it's best to view the entire URL by exploring the "View Page Source" option:

/w/load.php?lang=en&amp;modules=ext.uls.interlanguage%7Cext.visualEditor.desktopArticleTarget.noscript%7Cext.wikimediaBadges%7Cskins.vector.styles.legacy&amp;only=styles&amp;skin=vector

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

Determining the Number of Sub-Menu Items Using jQuery without Reliance on CSS Classes

I've scoured the depths of Google and SO in search of answers, but nothing quite fits the bill. My mission is to create a submenu without using classes, adding a style attribute to each individual <li> within the sub <ul> that sets a min- ...

Unlocking JSON object with AngularJS

I have saved JSON data in a separate file named Countries.Json located in a folder called ListofCountries within the View directory. Now, I am trying to display this data on button click using AngularJS. However, I am facing difficulties in passing the J ...

What is the best way to show only a section of a background in CSS?

Being a novice in HTML and CSS, I am facing the challenge of displaying an image as a banner with text overlay. Despite trying various methods, it seems impossible to achieve this directly with an image element, so I resorted to using CSS's background ...

Shopify's Ajax Cart experiences persistent loading issues when attempting to insert an <img> element within the code

I'm currently experiencing some difficulties when trying to insert a small image beneath the Check Out button on Shopify. I have attempted various locations without success, and I am using the Brooklyn theme. My approach involves using liquid tags an ...

CompletePage script, individual automatic scrolling

Having trouble with my fullPage.js implementation. I have a total of 5 sections and I'm trying to set different scrolling behaviors for each section. Specifically, I want Sections 1 through 3 to auto-scroll and Section 4.1 to 4.2 to use normal scroll. ...

Tips for resolving the error "Invalid value for style prop - must be an object" in a React.js and CSS scenario

Hey there, I'm currently working with module.css in React and facing an issue when trying to include the following line: <span style="--i:1;"><img src="https://i.postimg.cc/BQcRL38F/pexels-photo-761963.jpg" alt="not f ...

What is the best way to send a JavaScript array to a Perl script using AJAX?

Is it possible to convert a JavaScript array passed via AJAX into a Perl array? Accessing in Perl: @searchType = $cgi->param('searchType'); print @searchType[0]; Result: employee,admin,users,accounts It appears that the first value in the ...

What are the steps to create a dynamic navigation menu in Angular 2?

I have successfully implemented this design using vanilla CSS and JS, but I am encountering challenges when trying to replicate it in Angular 2. Setting aside routing concerns, here is the current state of my component: navbar.component.ts import { Comp ...

When the radio button is selected, show a variety of buttons

I'm facing an issue with rendering different buttons for two radio buttons within a view. Here is the rendered HTML code for the radio buttons: <input checked="checked" id="Isattending_0" name="Isattending" type="radio" value="Yes" /> <inpu ...

Tips for eliminating choices upon button press

How do I remove nested options inside a select element upon button click? I'm not sure where my mistake is, could it be in the for loop? var select = document.getElementById('colorSelect'); var options = document.getElementsByTagName(&ap ...

Endless loop of jquery textbox focus event

Currently, I am employing jQuery for validating textbox values. I have two textboxes - txt1 and txt2. For this purpose, I have created a jQuery function: $("#txt1").blur(function () { if ($("#txt1").val() == "") { $("#scarrie ...

Send the value of an li element using AJAX when clicked back to the original page

I'm currently working on passing the data-value of the < li > element that I clicked on to PHP within the same page. My approach involves using AJAX to send the data to PHP for querying purposes. While it seems like the AJAX request is functioni ...

Avoid making the check mark disappear in an SVG animation

After perfecting my CSS animation, I have the circle looping around to complete its shape while a check mark fills in the middle with a slight delay. Once both shapes are completed simultaneously, the check mark disappears. The reason for the disappearing ...

What is the best way to transform this PHP Object into an array?

I am working with a Javascript array that needs to be passed to a PHP script using Ajax. Inside file.js: var params = {}; params["apples"] = "five"; params["oranges"] = "six"; params["pears"] = "nine"; var ajaxData = {data : params}; fetchData(ajaxData); ...

Visualizing Data with HTML and CSS Chart Designs

I'm seeking assistance with creating an organization tree in a specific layout. I am having trouble replicating the tree structure shown in the image. I need help generating a similar tree structure where multiple levels are displayed, including ext ...

Is anyone else seeing a mysterious gray outline surrounding the image?

When visiting my website, I encountered an issue with the main menu. Specifically, when hovering over 'Populaire boeken', the first 2 menu items display properly with an image on mouseover. However, there seems to be a pesky grey border surroundi ...

Following the build in Angular, it only displays the index.html file and a blank screen

According to the information on Angular's official website, running the "ng build" command should generate files in the dist folder ready for hosting. However, after running this command, the index.html file is empty except for the page title. When yo ...

Resizing nested elements while maintaining consistent padding dimensions

If you're looking to create a sleek foundation for a 200px wide, 30px high editable combobox that can be easily used with angular binding or other JavaScript data-binding solutions, consider the following HTML code. However, there's a desire to m ...

Activate a dropdown menu following the selection of a date on a calendar input using vue.js

What I need to do is have two select fields, one for 'days' and the other for 'hours'. When a day is selected, the user should then be able to choose between two available time slots. If they choose day two, the available time slots sh ...

vue-router: A guide to disabling the outline property of the CSS in router-link

I am having trouble disabling the outline of a css link element. I successfully disabled the outline of a regular link using a css class, but I am unable to do so with the <router-link> element. How can I disable the outline for this? .page-link, .p ...