我正在生成window.location.href属性,其中的路径有时可能包含斜杠“ /”。
ImageButton.Attributes.Add("OnClick", "window.location.href='DynamicItemDetail.aspx?Partno=" & strItemCode & "&Decorloc='")
最终的字符串如下所示:
window.location.href="myurl.com/products.aspx?_Category=130&Partno=WWS-AWT/SWD&Decorloc="
不幸的是,由于项目代码包含斜杠,因此window.location重定向到根URL。无论如何,有没有告诉Javascript不要将斜杠视为子目录?
最佳答案
您将需要转义URL中的字符。
encodeURIComponent是您要寻找的。
var encodedItemCode = encodeURIComponent(strItemCode);
ImageButton.Attributes.Add("OnClick", "window.location.href='DynamicItemDetail.aspx?Partno=" + encodedItemCode + "&Decorloc='")
结果网址将是
myurl.com/products.aspx?_Category=130&Partno=WWS-AWT%2FSWD&Decorloc=
关于javascript - window.location.href中的正斜杠重定向到根URL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41860476/