Search code examples
javascriptregexurlbookmarklet

Combining bookmarklets to create a toggle between two URL


This is basically a variant of Combining Bookmarklets to create a toggle between HTTP and HTTPS?

I would like to combine the following two bookmarklets into one:

javascript:q=(document.location.href);location=location.href.replace('dp','gp/aw/d')
javascript:q=(document.location.href);location=location.href.replace('gp/aw/d','dp')

i.e. basically switch between two directories on a server.

This does not work (or only for one of the cases, i.e. it does not switch the 'dp' case).

javascript:q=(document.location.href);location=location.href.replace('dp','gp/aw/d').replace('gp/aw/d','dp');

Any (regex) improvements, e.g. so that the toggle will only catch the first occurrence of 'dp' in the url are welcome.


Solution

  • Do you mean

    javascript:(
      function() { 
        var loc=location.href;
        loc = loc.indexOf("dp")!=-1?loc.replace('dp','gp/aw/d'):loc.replace('gp/aw/d','dp');
        location=loc;})()