Reputation: 7766
I'm working on a bookmarklet that suggests links when you click on it. I imagine after clicking one of those links, when someone hits the back button, the intention is to go back to the list of links that the bookmarklet suggests.
However, I see no way to make this possible with a bookmarklet, which could be run over any web page. When you click a suggested link and then go back, the original page is brought up, without any of the Javascript from the bookmarklet, which normally brings up a list of links on top of the page.
Of course, the user can just click the bookmarklet again, but it would be nice if that wasn't required. Is there any way to do this, short of redirecting through a website hosted by me (which is a horrible solution IMO)?
Upvotes: 3
Views: 609
Reputation: 31285
If you can carry the data forwards when the user follows one of the suggested links (e.g. add #info=... to the link's URL), then the user could bring back the list of links by activating the bookmarklet again, without having to go back in their browser.
If the bookmarklet is on the toolbar, then this would be a 1-click operation. But it might raises one new issue: how to get fresh results when the bookmarklet is trying to show you results from the previous page!
Upvotes: 0
Reputation: 31285
If the browser changes page, then your bookmarklet will lose control. So one solution is not to change page.
You could open the suggested links in a frame or iframe. You can make the frame fill the window if you like, as long as there is some way to see the list of suggested links again.
Note this won't work on stackoverflow! They breakout of frames grrr.
Here is a somewhat related example: http://hwi.ath.cx/joeys_bookmarklets.html#splitpreview
Upvotes: 0
Reputation: 45132
How about a bookmarklet which composes some html into a string, then uses a combination of window.location
and document.write()
to display it?
var links = "<html><head><title>Suggested links</title></head><body>";
links += "<p><a href='http://example.com/'>Example Suggested Link</a></p>";
links += "</body></html>";
window.location = 'javascript:document.write("' + links + '");' ;
You have to be careful with your quoting, to make sure that the right string gets displayed, but after a quick test it seems to respect the back-button...
Here's a sample: http://jsbin.com/ebuko4 - it works for me in Firefox, but I haven't tested it elsewhere...
code is:
<html>
<head>
<title>JS Bin Sample bookmarklet</title>
</head>
<body>
<p><a href='javascript:(function(){ var l = "<html><head><title>Does this bookmarklet work?</title></head><body><p>"+ "<a href=\"http://example.com/\">Suggested link: Example.com</a></p>" + "<p><a href=\"http://example.net/\">Suggested link: Example.net</a></p>" + "</bo" + "dy></html>"; window.location = "javascript:document.write(%27" + l + "%27);"; })()'>Sample Bookmarklet - drag me to your toolbar</a></p>
</body>
</html>
Upvotes: 2
Reputation: 39095
No, I do not believe there is any way to do this - as far as I know it is not possible to automatically trigger bookmarklet javascript on page load - a user must click in order to execute.
If you insist on this functionality you will instead need to develop a browser extension. This will for example allow you to add a new persistent frame to the browser window in which you can display your suggested links. Mozilla/Firefox is quite straightforward to devlop for as it's all javascript and XUL (Mozilla's GUI markup language) based. Lots of tutorials and docs here: https://developer.mozilla.org/en/extensions
You can develop extensions for IE too, although I have no experience of this. Answers to this question How do you develop a plugin for IE? may help you get started.
Edit: I have thought of a possible workaround: your bookmarklet could create a new page in the current window, with the suggested links in a container, and the original page rendered in an iframe. Clicking on any of the suggested links would update the iframe. Without actually trying it, not sure what practical issues this may incur (e.g some sites - like SO - will not render in an iframe, and some links will have target="_blank" and open in a new window), or whether this format would be desirable to you.
Upvotes: 1