26

I'm looking for a simple bookmarklet to take the current URL of my website and refresh it with a couple of changes. For example:

  1. Take the current page: http://www.example.com/pages/
  2. and change it to: https://admin.example.com/pages/
  3. then load that new URL.

I tried searching for a bookmarklet that can do this but I couldn't find one. Can anyone point me in the right direction? Even a bookmarklet that does something like this that I can edit to suit my needs.

Erics
  • 803
  • 9
  • 23
garymc
  • 263
  • 1
  • 3
  • 5

3 Answers3

33

Just change window.location, e.g.

window.location=window.location.toString().replace(/^http:\/\/www\./,'https://admin.')

The full bookmarklet would then be:

javascript:(function() {window.location=window.location.toString().replace(/^http:\/\/www\./,'https://admin.');})()
endolith
  • 25,479
  • 34
  • 128
  • 192
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 14
    Perfect. Thanks for the record the complete code for the bookmarklet is: `javascript:(function() {window.location=window.location.toString().replace(/^http:\/\/www\./,'https://admin.');})()` – garymc Apr 22 '10 at 09:43
  • 1
    Can also use `window.location.href` or just `location.href` instead of `window.location.toString()`. – Kivi Shapiro Apr 27 '22 at 23:58
7

For example you could replace a part of the string using the replace method with a regular expression.

 javascript:location = location.href.replace(/http:/g, "https:" ) 

The above will assign the new string value to the location and trigger the page reload.

kim3er
  • 6,306
  • 4
  • 41
  • 69
  • 1
    Mine is a straight to SSL conversion. Kenny's switches sub domain as well. Remember to use the JavaScript sudo protocol in front of the JavaScript, to create a bookmarlet. – kim3er Apr 22 '10 at 09:31
  • 2
    sudo? I guess you are talking "pseudo-protocol" , that is that '''javascript:''' prefix. Seems the replyers do not like copy&paste ready-made examples. Users still have their work cut out. – dotbit May 20 '20 at 19:32
  • 1
    @dobit, correct I meant 'pseudo' not 'sudo'. – kim3er Mar 03 '21 at 16:29
2

This one will change the site name

javascript:(function() {document.title=prompt("Enter Page Title");})();
morganbaz
  • 2,997
  • 1
  • 17
  • 30
Dujuki
  • 21
  • 1