Opening external stylesheets in a separate window

Objective: Create a new window, add elements to it, and then apply CSS styles to the elements in the new window using an external style sheet.

Issue: While I am able to add elements to the new window and link it to an external style sheet in the head section, the styles from the CSS sheet are not being applied to the elements in the new window.

Fiddle https://jsfiddle.net/1f2per3s/

Javascript

$('.button').on('click', function() {
  let win = window.open();

  let link = document.createElement('link');
  link.setAttribute('rel', 'stylesheet');
  link.setAttribute('type', 'text/css');
  link.setAttribute('href', '/stylesheets/my.css');
  win.document.head.appendChild(link);

  let div = document.createElement('div');
  div.innerHTML = '<div> hello world</div>';
  win.document.body.appendChild(div);
});

HTML

<div class="container">
  <div class="button">
    test element
  </div>
</div>

Answer №1

When examining the address bar, you will notice that the URL does not belong to your website (For instance, in Chrome, it displays as about:blank). The issue arises because the path to your stylesheet is relative, causing it to be inaccessible.

To resolve this, you can utilize the full, absolute URL:

link.setAttribute('href', 'http://example.org/stylesheets/my.css');

https://jsfiddle.net/3xhrnpsa/ (bootstrap's CSS is used for the demo, resulting in a sans-serif font for the text)

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

Do you create a dedicated CSS file specifically for Internet Explorer?

I am currently exploring the best solution for my current situation. Previously, I utilized CSS3 to create rounded corners and drop shadows, but encountered compatibility issues with IE8. As a result, I had to implement CSS3pie to support IE8. However, th ...

What is the best method for choosing the final element with a specific class in each row?

Within each row, there are various cells that have the class .meow. How can I target the last .meow element in each row? The provided code currently selects all existing .meows... $("tr").each(function(){ $(".meow").css("border", "3px solid red"); ...

What is the best way to horizontally align Bulma's level items on smaller screens?

My website's footer features a level layout with left and right sections that follow the guidelines provided in theBulma docs. Everything looks good on larger screens. However, on smaller screens, all items in the right section are stacked vertically ...

The scroll function remains unchanged when using overflow-y scroll and border radius

I need help with styling a div that will contain a large amount of text on my webpage. I want to use the overflow-y property for scroll, but the issue arises when trying to style the scroll bar with a radius. Check out this jsfiddle link for reference: ht ...

How to Monitor Store Changes within a Vue File Using Vue.js

I am working with 2 vue files, header.vue and sidebar.vue. Both components are imported into layout.vue. Here are the steps I am following: 1. Initially, when the page loads, I update the store in header.vue with some values inside the created hook. 2. ...

Is there a way to retrieve a different value within the JavaScript code (imagepicker)?

I need help with setting up a page where users can choose an image to forward them to another page. However, I'm facing an issue when the user chooses two images at once. Can anyone provide ideas on how to handle forwarding in this scenario? You can c ...

Retrieves MySQL SQL findings and converts them into a JSON structure

I'm in the process of integrating Typeahead.js into my website. The Typeahead.js plugin will retrieve data from a remote page that returns JSON, similar to: http://example.org/search?q=%QUERY Here's the PHP code I've written for my site: ...

`Why won't my xpath expression function properly?`

<DOC NUMBER=1> <DOCFULL> --> <br><div class="c0"> <p class="c1"><span class="c2">Document 1 of 3</span></p> </div> <br><div class="c0"> <br><p class="c1"><span class="c2"&g ...

How to retrieve the input value in React Autosuggest

I recently began my journey into learning JavaScript and React. Currently, I am working on creating a simple table with material design. The goal is to be able to add rows to the table through a form popup and delete rows using an icon within each row. On ...

Can the individual headers of an ag-grid column be accessed in order to apply an on-mouseover event with a specific method?

In my current project, I am utilizing ag-grid to create a dynamic web-application that combines tools from various Excel files into one cohesive platform. One of the Excel features I am currently working on involves displaying a description when hovering ...

various stunning galleries accessible from a single page of thumbnail images

I'm trying to create a unique gallery experience on my website. I have a set of 6 images, each featuring a different house. What I want is for each image, when clicked, to open up a fancybox gallery showcasing 4 more detailed photos of the same house. ...

Managing input jQuery with special characters such as 'ä', 'ö', 'ü' poses a unique challenge

Hey there, I'm running into a bit of trouble with my code and I can't figure out why it's not working. Here's a brief overview: I created my own auto-suggest feature similar to jQuery UI autosuggest. Unfortunately, I'm unable t ...

The navigation bar on the website highlights vibrant green arrows next to the

I'm having trouble implementing a bootstrap menu on my website using the code provided by Bootstrap. The menu is not displaying correctly and instead showing small green UL arrows. I have JavaScript and Bootstrap scripts in my project. Is there a way ...

Developing uniform resource locators with jQuery

As I work on developing my webapp, I have made use of the tag in my JSPs to ensure that all links within the application - whether they lead to pages or resources like images and CSS - always stem from the root directory rather than being relative to the ...

"When attempting to output HTML using PHP, be sure to include a backslash () before special characters like

One issue I am facing is with a PHP code that retrieves HTML content from the database and uses echo to display it on my page. The problem arises when I use echo, as the HTML output ends up looking like this: <div class="\&quot;schema-faq- ...

Can multiple `npm install` commands run at the same time?

Is it possible to have concurrent runs of npm install, or will they conflict on shared resources and create potential race conditions? Analyzing the code might not provide a clear answer, but for those working on developing npm, this is likely an implicit ...

Return to a mention of a tree reference

Here is an example code snippet: <table> <tr> <td></td> <td></td> <td> <table> <tr> <td><a href="#" class="fav">click me</a></td> ...

avoiding the duplication of effects on an object when new objects are added via ajax

Currently, I am facing a minor issue with an application that I am working on. The problem arises on a particular page where the user can edit a document by dragging modules onto the canvas area. Upon loading the page, JavaScript causes the modules to be ...

What could be the reason for my inability to retrieve req.user.username with passport.js?

I recently started using passport.js for authentication and I'm encountering an issue. When I log in via Google, the only information available to me through req.user is the user id. I have provided my passport setup code, along with the routes, hopin ...

Using jQuery, remove any white spaces in a textbox that are copied and pasted

There is a textbox for entering order IDs, consisting of 7 digits. Often, when copying and pasting from an email, extra white spaces are unintentionally included leading to validation errors. I am looking for a jQuery script to be implemented in my Layout ...