Is it possible to add a query param programmatically?

When I’m modifying a cell, I sometimes want to make a variable persistent, so I use the URL params pattern (generate a link with the params and click it):

I was wondering if this could be done in one step (changing the select, automatically updates the URL param), without having to click the link. This works on the developer console, but because of how Observable runs it doesn’t work on a cell:

{
  const url = new URL(location.href);
  url.searchParams.set("value", "b");
  location.href = url.toString();
}

No, notebook scripts can’t change the parent context’s history.

You can invoke a top-level navigation by using something like

htl.html`<a href="${settingsURL}">`.click()

which would trigger the next time the cell reevaluates, but obviously that’s a terrible idea. :slight_smile:

The alternative would be to store your settings in the hash, like

htl.html`<a href="#${currentValue}">`.click()

but then you’ll be breaking cell links, and cell links will break your settings.

For what it’s worth, I think being explicit about when to persist new settings is a good thing. I usually expose the configuration URL as a share link (like “Share these settings”) which I feel should be reasonably intuitive.

1 Like