0

I'm creating a chrome extension. It's a theme for a website and I'm making changes with css but also javascript. The javascript part simply exchanges a broken link with the working one and gives it another text. The code works on a fresh load of the page, but if I navigate to any other part of the site, it doesn't work anymore. It only starts working again when I reload, or upon opening a link in a new tab.

How can I change the code so it runs even after navigating to other parts of the site within the same tab?

var bookmarks = document.querySelector(".class li:nth-of-type(n) a");
bookmarks.innerHTML = "bookmarked";
bookmarks.href = "/user/bookmarks";

The queryselector stays the same after navigating, I tested it with css. I tried this next --

var bookmarks = document.querySelector(".class li:nth-of-type(n) a");
window.onload = function() {
    bookmarks.innerHTML = "bookmarked";
    bookmarks.href = "/user/bookmarks";
};

but to no avail. I'm really at a loss here as to what is going wrong.

echo
  • 1
  • 1
  • It's hard to say without more context as to what HTML elements you're manipulating and when your code is called. My guess is that you're injecting this code on page load and that the website uses AJAX do dynamically change the page content without changing the whole page. – Domino Jun 05 '17 at 22:45
  • Perhaps this will help you: https://stackoverflow.com/a/39508954/2813263 – Domino Jun 05 '17 at 22:47
  • Yeah, it's as you suspected -- the website indeed uses AJAX and that's the reason the code isn't working after navigating. – echo Jun 05 '17 at 23:23

0 Answers0