簡體   English   中英

使用Javascript通過頁面重定向

[英]Page redirect with path using Javascript

如何使用帶有路徑的Javascript重定向請求?

my-domain.com/abc to www.other-domain.com/abc

其中“ abc”是請求路徑。 還可以轉發查詢參數和片段標識符嗎?

這應該在服務器端完成,但是如果要使用javascript,可以

location.href = location.href.replace(/(https?:\\/\\/)[^/]+/,'$1'+'www.other-domain.com');

注意:regex包括使用與url相同的協議進行轉發。.如果不包括該協議,它將嘗試重定向到與頁面相同的域,但使用“ www.other-domain.com/ ..”。作為相對路徑。 因此,您必須包括該協議。 另外,您也可以像這樣硬編碼:

location.href = location.href.replace(/(https?:\\/\\/)[^/]+/,'http://www.other-domain.com');

這應該可以解決您的問題,但是強烈建議您在服務器級別進行。

window.location = 'www.other-domain.com' + (location.pathname + location.search);

就像是:

var url = "http://www.other-domain.com" + location.path + location.search + location.hash;
location.href = url;

有關更多信息: http : //www.w3schools.com/jsref/obj_location.asp

window.location.replace("http://www.other-domain.com/abc");

請參見Window.location-Web API接口| MDN了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM