Henry The Least
Henry The Least

Reputation: 649

Open new webpage with currently open site using JavaScript bookmarklet

I am trying to create a JavaScript bookmarklet. Basically, first I will go to site1.com, I will then click on the bookmarklet and it will take the URL of the site1.com, and open site2.com/go.php?go=site1.com

So, the JavaScript needs to add site1.com to site2.com/go.php?go= and then open the webpage at site2.com/go.php?go=site1.com

Upvotes: 1

Views: 102

Answers (1)

Lucas
Lucas

Reputation: 541

This bookmarklet will do exactly what you’re asking for:

javascript:window.location='http://site2.com/go.php?go='+escape(window.location.hostname);

The potential problem with that bookmarklet is that it will send you to http://site2.com/go.php?go=site1.com whether you’re coming from https://site1.com/secret.html?query=etc or http://site1.com. That is, no matter where on site1.com you are when you click the bookmarklet, it will still send you to http://site2.com/go.php?go=site1.com.


So what I suspect you might want instead is:

javascript:window.location='http://site2.com/go.php?go='+escape(window.location);

This will allow the query string to show exactly where you were on site1.com when you clicked the bookmarklet, plus it includes the URI scheme (the http:// or https:// parts of the site1.com address). Using the same two examples respectively, this bookmarklet would send you to http://site2.com/go.php?go=https://site1.com/secret.html?query=etc¹ and http://site2.com/go.php?go=http://site1.com/


  1. It will actually percent-encode the site1.com address and send you to http://site2.com/go.php?go=https%3A//site1.com/secret.html%3Fquery%3Detc because the site1.com address contains several characters that don’t travel well as URI fragments unless encoded.
  2. Actually to http://site2.com/go.php?go=http%3A//site1.com/ for the same reason.

Upvotes: 1

Related Questions