1

I often work with three variations of a web page, 1) a dev url, 2) a preview/staging url, and 3) and live url.

I would like to create a link (bookmarklet?) that I can then add to my bookmarks bar that will change part of the URL string (basically everything before the page name) and then load the resulting URL in a new tab (or same tab if easier).

Example: working on a page with the Dev URL:

https://dev.mysite.com/cf#/content/www/page1.html

I would like to be able to click the link and have the page reload and return the following staging URL in the same or new window/tab:

https://preview2.mysite.com/page1.html

and then if I click the link again, I would like the page to reload and return the following live url in the same or new window/tab:

http://www2.mysite.com/page1.html

and then if I click the link again, I would like the page to reload and return the following dev url in the same or new window/tab:

https://dev.mysite.com/cf#/content/www/page1.html

So, I would basically like to avoid a lot of cut/copy and paste when I am changing through these variations of the url while developing, testing, and visiting the live versions of the page.

Here is where I am so far.. Stuck on the most basic aspect of it, if on the dev page, reload to preview page:

A variation of the method by joewiz.org "fixing-urls-with-dns-errors" and this one from Stack user63503 "how to replace part of the URL with JavaScript?" and after also trying str.replace from W3Schools.

javascript:(function(){window.location.url.replace("dev.mysite.com/cf#/content/www/","preview2.mysite.com/");})();

or

javascript:(function(){window.location.pathname.replace("dev.mysite.com/cf#/content/www/","preview2.mysite.com/");})()

Thats just one of the steps as you can tell and I'm already stuck. I have tried so many variations but I cannot replace a specific part of the url, let alone add the rules to reload to dev, preview, live depending on where the current browser is.

I realize that very similar questions have already been asked, but I'm afraid that I am unable able to extrapolate actionable information from the ones that I have found. However, please let me know if you feel a previous post is relevent and I'll head over there and study-up.

Thank you all so much!!

Community
  • 1
  • 1
Gaberham
  • 43
  • 1
  • 5
  • try location.href instead of location.pathname. Also check the console for errors. Also in some browsers you MUST run your javascript from a bookmark, not from location bar – mplungjan Apr 22 '15 at 15:50
  • @mplungjan Thanks, I tried,javascript:(function(){window.location.href.replace("preview2","www2");}) but still no go. – Gaberham Apr 22 '15 at 18:04
  • Any errors in the console? – mplungjan Apr 22 '15 at 18:05
  • Sorry, I should have responded to that part too :). No errors, it just doesn't do anything. If I remove a character or mess up the code and press the link I get an error for what it's worth. It's just not firing. I also tried it with other websites to try to replace parts of a URL, but still no luck or errors. – Gaberham Apr 22 '15 at 21:01

1 Answers1

6

Your code should work.

But try this

javascript:(function(){var loc=location.href;loc=loc.replace('dev.mysite.com/cf#/content/www/','preview2.mysite.com/'); location.replace(loc)})()

Remember to copy this into the URL of a real bookmark - I normally send people a web page they can drag from so the quotes are indeed important:

<a href="javascript:(function(){var loc=location.href;loc=loc.replace('dev.mysite.com/cf#/content/www/','preview2.mysite.com/'); location.replace(loc)})()">ReplaceUrlBM</a>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    This is Awesome! I juts couldn't get my code to work, but this works perfectly! How on earth did you figure this out? This is what I ended up using: javascript:(function(){var loc=location.href;loc=loc.replace(/.*\/us/,"https://dev.mysite.com/cf#/content/www/us"); location.replace(loc)})() and so on. – Gaberham Apr 23 '15 at 13:38
  • I have written bookmarklets for more than 15 years ;) – mplungjan Apr 23 '15 at 13:40
  • That will do it! :) Also, slight change, I used single quotes instead. This allowed me to put the link on a page if needed and to drag and drop into the browser bar instead of editing and copy/paste the code into an existing bookmark. ie ...'dev.mysite.com/cf#/content/www/us')... Thanks again!! – Gaberham Apr 23 '15 at 13:51
  • Some browser no longer allows javascript protocol in the Url bar :( – mplungjan Apr 23 '15 at 13:53