I have trouble describing my problem because I am still somewhat of a beginner in HTML and English is not my native language. Please bear with me.
I have a website that has somewhat of a blog. Each week I change out the iframe link to the content of just the blog, navigation and introduction stays the same.
<div class="blog"><iframe src="Blogs/REVblog20211023.html" name="myBlogFrame" scrolling="yes"></iframe</div>
Older entries are loaded in the same iframe window.
<a href="Blogs/REVblog20211002.html" target="myBlogFrame"><li>October 2nd<br /> <img src="blogs/Media/spacer.gif" width="100%" height="1px" alt=""/>What drives obesity?</li></a>
Is it possible to add something to the URL, so that an older blog is automatically opened?
Like this: https://reversing-insulin-resistance.com/blog.html?#iframe:Blogs/REVblog20211002.html
Obviously some code would have to be added to the website to make the automatic switch possible.
You can. Basically on page load, you check if that bit is there and do something with it if it is. I'd use a query (?) instead of a hash (#) though, as hash is generally used for jumplinks that the browser can do automatically and you don't necessarily want to overload it. (You can if there is a specific reason to do so, it's just usually better to use a query instead.)
If you put your script as the last element in your body (and the HTML isn't dynamically created with JavaScript), that should be sufficient.
If you use a URL ending like this: ?iframe=path/to/older.html
Then, it's just be something like:
const domain = 'https://example.com';
// gives "iframe=path/to/older.html"
const query = location.search.slice(1);
// in case you ever add more query params in the future, it's good
// to process them properly
// this will give an array like [["iframe", "path/to/older.html"]]
const pairs = query.split('&').map(pair => pair.split('='));
// find just the iframe path
// if the query isn't there, iframePath will just be undefined
const [, iframePath] = pairs.find(([key]) => key === 'iframe') || [];
// if we got an iframePath, assign it as the URL of your iframe
if (iframePath) {
document.querySelector('iframe').setAttribute('src', domain + '/' + iframePath);
}
And that should do the trick. If you have more than one iframe on the page, you'll want to adjust the document.querySelector() to a different selection to get the correct iframe. And obviously, set the domain properly.
If they happen to be the same domain as the page with the iframe, you could also just do location.origin or something instead of hard-coding it.