1

I want the below javascript to update the specific css link bootstrap.min.js if the page is named mypage.html.
How would I do that?

<html>
  <head>

  <link rel="stylesheet" type="text/css" href="/css/bootstrap377.min.css"/>
  <link rel="stylesheet" type="text/css" href="/css/some_other_stylesheet.css"/>

  <script type="text/javascript">
    var filename = location.href.split("/").slice(-1); 

    if (filename == "mypage.html") {
      HOW DO I DO THIS:  update the above stylesheet link to be bootstrap400.min.css (or should this script be before the css link?)
    }
  </script>


  </head>
  <body>
    this is a test
  </body>
</html>
2
  • You can't access the filename from the HTML Commented Feb 3, 2017 at 17:01
  • My example already accesses the filename, I need the script to modify the css link Commented Feb 3, 2017 at 17:23

1 Answer 1

1

Use document.querySelector to get a reference to the link element, then change the href property the usual way.

var filename = location.href.split("/").slice(-1); 

if (filename == "mypage.html") {
  var link = document.querySelector("link[href='/css/bootstrap377.min.css']");
  link.href = "/css/bootstrap400.min.css";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent! However I should have mentioned that my real world application has multiple css links so the only link to be updated is the link that contains "bootstrap.min.css". How would I do that?
@user3302169 I updated my solution. You may want to edit your question with the information that there are multiple links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.