I am complete beginner in JavaScript.
At the moment, I am creating a JS snippet for a bookmarklet in Chrome (103.0.5060.114
) on Windows 10 Pro. This bookmarklet should open to the bottom of the webpage at an arbitrary URL.
Problems
While my bookmarklet does open the given page, it wrongly remains at the top of the page.
Furthermore, while it opens from any webpage (like https://www.google.com
) to which I navigate, it fails to open from a newly created tab. A new tab initially displays my default homepage, which is Google, but the URL (https://www.google.com
) is notably absent from its slot:
Code
Here are my two custom functions, which figure into the full snippet below.
Function window_scroll_to_bottom()
function window_scroll_to_bottom() {
// Save the existing scroll settings.
save_scroll_restoration = history.scrollRestoration;
// Override any autoscroll in Chrome.
history.scrollRestoration = "manual";
// Scroll to the bottom of the current page, per https://stackoverflow.com/a/29971996.
window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight);
// Restore scroll settings.
history.scrollRestoration = save_scroll_restoration;
}
Function window_open_to_bottom()
function window_open_to_bottom(...args) {
// Save existing settings on load.
save_on_load = window.onload;
// Scroll to bottom on load.
window.onload = window_scroll_to_bottom;
// Open the window, according to the given arguments.
window.open(...args);
// Restore settings on load.
window.onload = save_on_load;
}
Full Snippet
Here is my readable .js
snippet
javascript:
url = "https://stackoverflow.com/q/73326280";
function window_scroll_to_bottom() {
save_scroll_restoration = history.scrollRestoration;
history.scrollRestoration = "manual";
window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight);
history.scrollRestoration = save_scroll_restoration;
};
function window_open_to_bottom(...args) {
save_on_load = window.onload;
window.onload = window_scroll_to_bottom;
window.open(...args);
window.onload = save_on_load;
};
window_open_to_bottom(url, "_self");
which I condense into a single line
javascript: url = "https://stackoverflow.com/q/73326280"; function window_scroll_to_bottom() {save_scroll_restoration = history.scrollRestoration; history.scrollRestoration = "manual"; window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight); history.scrollRestoration = save_scroll_restoration}; function window_open_to_bottom(...args) {save_on_load = window.onload; window.onload = window_scroll_to_bottom; window.open(...args); window.onload = save_on_load}; window_open_to_bottom(url, "_self");
and then paste into the URL field in Chrome's bookmark editor:
Background
My company has some dashboards hosted by a third party ("Dashboard Service"), each of which is located at a unique URL. For example, the 2
nd dashboard is found at https://mycompany.dashboardservice.com/Dashboard/Index/2
.
These dashboards are large and render their widgets piecemeal, from top to bottom. If one scrolls immediately to the bottommost widget, then everything above it will render too. Otherwise, when one wishes to scroll downward, one must wait for every subsequent widget to render.
This rendering takes forever and bogs down our meetings. Management needs a stopgap solution, while we await a more permanent solution by Dashboard Service.
Goal
I wish to create a bookmark(let) in Chrome that targets the bottom of each dashboard. Left-clicking this bookmark(let) should open it in the current window; but right-clicking it should offer the option of opening in a new tab or window.
Challenge
There exists no HTML anchor like id="dashboard-bottom"
at the bottom of the page. Worse yet, the widgets (and other regions) are also unanchored by anything like id="widget-5"
. Finally, there is no universal anchor like #bottom
that automatically targets the bottom of any page.
Thus, I cannot simply bookmark the following URLs for (say) the 2
nd dashboard which has 5
widgets:
- The dynamic
https://mycompany.dashboardservice.com/Dashboard/Index/2
#dashboard-bottom
. - The static
https://mycompany.dashboardservice.com/Dashboard/Index/2
#widget-5
. - The universal
https://mycompany.dashboardservice.com/Dashboard/Index/2
#bottom
.
Idea
In response to the #bottom
problem, this suggestion
In my opinion, it's better to implement this in JavaScript and definitely jump to the bottom of the page, instead of relying that the CSS styling on a link makes it always appear at the bottom of the page.
This JavaScript snippet will scroll to the bottom of the document:
document.body.scrollIntoView(false);
led me to this Javascript solution
Below should be the cross browser solution. It has been tested on Chrome, Firefox, Safari and IE11
window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight);
which uses window.scrollTo()
to scroll to the bottom of the current page. This approach is supplemented by these considerations
Browsers such as Chrome have a built-in preset to remember where you were on the page, after refreshing. Just a
window.onload
doesn't work because your browser will automatically scroll you back to where you were before refreshing, AFTER you call a line such as:window.scrollTo(0, document.body.scrollHeight);
That's why we need to add:
history.scrollRestoration = "manual";
before the
window.onload
to disable that built-in feature first.
for history.scrollRestoration
with window.onload
:
Code:
<script> function scrollToBottom() { window.scrollTo(0, document.body.scrollHeight); } history.scrollRestoration = "manual"; window.onload = scrollToBottom; </script>
All this inspired me to investigate bookmarklets, which can function like bookmarks for lay users. If I can use the scrolling solution in tandem with a solution like this
At the moment (Chrome 39) I use this code to open a new tab:
window.open('http://www.stackoverflow.com', '_blank', 'toolbar=yes, location=yes, status=yes, menubar=yes, scrollbars=yes');
which opens the page in the first place, then my goal should be attainable.
Summary
I wish to write a concise snippet (and ideally canonical) that should define a Chrome bookmarklet that
- opens in Chrome
- just like a normal bookmark (with the option of a new tab or window)
- to the webpage at an arbitrary URL
- starting from any window or tab (including the "blank" homepage) in Chrome; then
- scrolls reliably to the bottom of that page, and
- remains at the bottom without "jumping" back to the top; but
- permits the user afterward to manually scroll from there.
Browser agnostic solutions are also welcome, so long as they satisfy the other requirements above.
Update
After learning here that window.location.href
= ...
is preferred to window.open
(...)
, I realized that setting
history.scrollRestoration = save_scroll_restoration;
might actively trigger an event: returning to the position dictated by Chrome's autoscroll.
After removing save_scroll_restoration
from window_scroll_to_bottom()
function window_scroll_to_bottom() {
history.scrollRestoration = "manual";
window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight);
}
and replacing window_open()
with window.location.href
in the .js
snippet
javascript: url = "https://stackoverflow.com/q/73326280"; function window_scroll_to_bottom() {history.scrollRestoration = "manual"; window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight)}; window.location.href = url; window_scroll_to_bottom();
I see the webpage open and "flicker" to the bottom before "jumping" back to the top. This seems to show progress, but I still can't get the webpage to "stay" at the bottom.
As before, the webpage will not open from a new tab.