Combining Bookmarklets to create a toggle between HTTP and HTTPS?

和自甴很熟 提交于 2019-12-06 08:33:10

问题


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!



来源:https://stackoverflow.com/questions/12068378/combining-bookmarklets-to-create-a-toggle-between-http-and-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!