我正在构建一个可以更改url的一部分并可以打开更新的URL的javascript书签。以下是我写的代码。在通过javascript打开之前将https添加到任何URL
var str = "www.myweb.com/in/products/index.aspx";
var pattern2 = new RegExp('www.myweb.com','i');
var str1 = str.replace(pattern2, 'https://www-stg.myweb.com:60002');
window.location.href = str1;
这导致HTTP // www-stg.myweb.com:60002 /英寸/产品/的Index.aspx这在不正确。我想添加https://
之前www-stg.myweb.com
如果我alert
它或console.log()
它,它会显示正确的东西。但浏览器一旦提交,即添加http
。
如何克服这一点?
它应该是'window.location.href = str1;',不是吗? – Regent 2014-09-12 13:05:02
@ Regent:对不起,我已更新它。仍然是这个问题 - 它不识别'https://www-stg.myweb.com:60002'中的冒号,并且它打开为'https // www-stg.myweb.com:60002/in/products/index .aspx' – 2014-09-12 13:10:49
看起来像是在[fiddle]中工作(http://jsfiddle.net/abt9srbm/)。 – Regent 2014-09-12 13:19:03