简体   繁体   English

Javascript重定向并提取根域名以进行重定向

[英]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. 目标是用户可以输入计算机所在的端口号(例如端口1058”,该脚本只需将其重定向到该端口即可简化工作。

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. 假设他们正在浏览的网页是“ www.redirectwebsite.ca/index.html,然后用户单击远程访问链接,该链接会在Javascript框中提示他们要浏览的端口。用户将输入端口1058,然后该脚本将收集域地址,并在其后面加上“:”以及他们刚刚输入的端口号,在这种情况下,该地址将为“ www.redirectwebsite.ca:1058),然后浏览器将自动重定向到此页面在新窗口中。

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. 目前,我已经对其进行了设置,以便可以指定域,但是我希望它能够使javascript拉引引荐页域(因为我们设置了多个域名),因此它是完美的重定向。

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 ". 我需要弄清楚如何使文档拉到用户正在浏览的域,并在我当前拥有“ http://permanentaddress.com ”的位置添加它收到的URL。 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". 结果将是,如果用户通过域“ domain1.ca”访问该网站,则重定向会将他们指向“ domain1.ca:1058”,如果他们通过“ domain2.ca”进行访问,则重定向将指向他们到“ domain2.ca:1058”。

You can use javascript to get the domain like so: 您可以使用javascript来获取域,如下所示:

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. 您可能需要进行其他检查,以查看它们是否位于https上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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