14

Essentially I'm trying to

  1. navigate to a webpage
  2. wait for that webpage to load
  3. execute a JS function/alert/whatever on that page

all from a single bookmarklet. Is this possible? I can't seem to get onload to work for me, but that may be because of my own personal failings here.

1
  • lets see your code - Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Reproducible Example.
    – sao
    Oct 28, 2019 at 13:56

4 Answers 4

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

11

The simplest way I found to do this without needing Greasemonkey or something similar is to write your JS so that it checks to see if it is on the appropriate page, and goes there if it isn't. If it is on the page, then it executes the JS/alert/whatever. You have to use the bookmarklet twice, but you just need one bookmarklet, and it may still be quicker/easier the user doing the clicking/whatevering him or herself. So the code would look like this:

if(this.document.location.href != "[url]") { //Are we on the page yet? 
  this.document.location.href = "[url]"; // If not, go there
}
else {
  if (document.readyState === "complete") { //Wait for the page to finish loading
   // DO STUFF
  }
}
1
4

You want to install the Greasemonkey extension for Firefox. (or gm4ie for IE, or greasemetal for Chrome (PersonalizedWeb also works in a much simpler way for Chrome), greasekit for Safari, or user.js for Opera)

Greasemonkey lets you do exactly this... run a script automatically on every page load (you can choose what pages/sites it loads on)

Otherwise you will need to click your bookmarklet on every page load in order to run your script.

4
  • 2
    So Greasemonkey (or its equivalent in $browser) is the only solution for this? My goal was to just make a bookmarklet I could share with other people, instead of requiring an add-on in each case. For some context, the problem I'm tryign to work around is my school's website. The web designers thought it would be a brilliant idea to make all links on the site merely JS links which through some cryptic combination of PHP and AJAX update the webpage. Unfortunately, because of the way they did this it's currently impossible to link to, say, your classes' homework page.
    – scikidus
    Feb 16, 2010 at 3:09
  • 2
    Instead you have to say "Go to website, then click here, then click here, then click here." My hope was to make a bookmarklet which would load the website and once the page loaded run the JS links necessary to retrieve the desired page.
    – scikidus
    Feb 16, 2010 at 3:10
  • Ah,@scikidus I see... well unfortunately it sounds like the site is out of your control. You could create/use a bookmarklet, but there is no way to trigger it to run automatically (without) resorting to a Greasemonkey-type solution. Is there a chance you can talk with the staff at your school to offer up a fix for the site? As for the original developers (-1 for JS-only links)
    – scunliffe
    Feb 16, 2010 at 13:44
  • This answer is out of data. Check out wiki.greasespot.net/Cross-browser_userscripting for a more recent overview of alternatives.
    – Rob W
    Jun 26, 2013 at 21:57
2

Given there's no better solution, I thought I'd toss out that Opera natively supports user scripts to run on every page load. From there, you could have the script check the current url, and run if on appropriate page.

See here for documentation

0

Another option is to call window.open(...), and use the window object to manipulate the window. It is also possible to navigate multiple pages this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.