简体   繁体   中英

Javascript redirect and Extract root domain name for redirect

So let me try and explain what exactly I'm trying to do.

Currently I have a web page designed, with a button that when clicked, a prompt box is displayed asking the user what port they want to navigate to (This is for a personal website with many remote computers residing at different ports on the network). What the goal is, is that the user can enter the port number their computer is located at (for example port 1058", and the script will make the work easy by simply redirecting them to that port.

Say the webpage they are browsing is "www.redirectwebsite.ca/index.html, the user then clicks the remote access link which prompts them with a javascript box asking which port they want to browse to. The user would enter port 1058, and the script would gather the domain address, append the ":" along with the port number they have just entered, in this case the address would be "www.redirectwebsite.ca:1058), and then the browser would automatically redirect to this page in a new window.

I currently have it set up so that the domain can be specified, but I would like it so that javascript pulls the referring page domain (as we have multiple domain names set up), so that it is a flawless redirection.

The code I currently have is below. Thanks in advance for the help.

        <script type="text/javascript">
    function portredirect() {
        var port = prompt("Please enter the port # you would like to connect to, and press OK. You will be automatically redirected", "Enter port # here");
        if (port != null && port != "") {
            window.open("http://permanentaddress.com:" + port, '_blank');
        }
    }
</script>

I need to figure out how to have the document pull the domain the user is browsing at, add that url it receives in where I currently have " http://permanentaddress.com ". Any thoughts on how I might achieve this? The result would be that if the user accessed the website via the domain "domain1.ca", the redirect would refer them to "domain1.ca:1058", and if they accessed it via "domain2.ca", the redirect would refer them to "domain2.ca:1058".

You can use javascript to get the domain like so:

alert(document.domain);

Your function would look like this:

<script type="text/javascript">
    function portredirect() {
        var domain = document.domain;
        var port = prompt("Please enter the port # you would like to connect to, and press OK. You will be automatically redirected", "Enter port # here");
        if (port != null && port != "") {
            window.open("http://" + domain + port, '_blank');
        }
    }
</script>

You might have to put an additional check in place to see if they are on https.

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