Ron I Ron I - 5 months ago 7x
Javascript Question

Creating and setting a link element in a new html page in a new window

I am trying to accomplish the following and my approach isn't working currently. Hopefully the SO community can help! Thanks in advance.

Goal: user clicks a button on page A and it opens page B and sets page B's link element in the head to point to a css file.

Problem: I get this error in the console:

Uncaught TypeError: Cannot read property 'head' of undefined



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


$('.button').on('click', function() {
let win =;
let link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('href', '/stylesheets/my.css');



You need to execute the function:

let win =; // Notice the parenthesis

And document.head refers to nothing. Instead, do :


If you inspect the source of the new window, you'll see that link tag you created.