Combining Bookmarklets to create a toggle between

2019-04-12 11:58发布

问题:

By searching here, I now have two bookmarklets, which switch from/to HTTP/HTTPS :-

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

And ...

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

But is there a way, please, to combine them into a single bookmarklet, which will toggle from one to the other according to whichever is presently loaded?

回答1:

This thread is a little old, but I haven't found a better answer anywhere else.

javascript:((function(){window.location=location.href.replace(/^http/i,"https").replace(/^http\w{2,}/i,"http");})())

is fully formed bookmarklet code that should work in any browser.



回答2:

location.href.replace(/^http/i,"https").replace(/^http\w{2,}/i,"http")


回答3:

if (window.location.protocol == 'http:') window.location.protocol = 'https:'
else window.location.protocol = 'http:'

Then all you'd need to do is format that as a bookmarklet. Enjoy!