简体   繁体   中英

Javascript Redirect - Keep Path and Variables, Switch Domain

I am trying to do a mobile redirect using javascript. Here is what I am trying to accomplish:

Normal View: https://secure.example.com/checkout/Checkout.aspx?a=1&b=2&c=3&d=456789

Redirected to

Mobile View: https://differentdomain.com/mobile/Checkout.aspx?a=1&b=2&c=3&d=456789

        <script type="text/javascript">
            function TN_mobileUrlOverride()
                {
                    TN_mobile.DroidUrl = TN_mobile.IphoneUrl = TN_mobile.BlackBerryUrl = "https://example.com/mobile" + window.location;
                }
        </script>
        <script src="http://s3.amazonaws.com/TNService/Js/mobile.js"></script>

When I use window.location, it adds the entire domain resulting in: https://example.com/mobile/https://differentdomain.com/mobile/Checkout.aspx?a=1&b=2&c=3&d=456789

When I use window.location.path, it doesn't carry the variables, only the file: https://differentdomain.com/mobile/Checkout.aspx

How can I keep the file and url parameters while replacing the domain?

Thank you!

Use location.host :

window.location.host = "differentdomain.com";

Assuming you need TN_mobile.DroidUrl = TN_mobile.IphoneUrl = TN_mobile.BlackBerryUrl to all equal the desired url, do:

var url = new URL(window.location);
url.host = "differentdomain.com";
TN_mobile.DroidUrl = TN_mobile.IphoneUrl = TN_mobile.BlackBerryUrl = url.toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM