简体   繁体   English

如何在 Javascript 中传递 url 中的字符串?

[英]How to pass a string in url in Javascript?

I'm trying to make an application that accesses different sites inside of an iframe from a private JSON dns server.我正在尝试创建一个应用程序,该应用程序从私有 JSON dns 服务器访问 iframe 内部的不同站点。 I have completed the main code, but now I am trying to make it able to be accessed through the url.我已经完成了主要代码,但现在我正在尝试使其能够通过 url 访问。 (Ex. https://thissite.info?url='test.json'&dnsserver='https://thissite.info') I also want it to default to a value if the variable is not defined in the url. (Ex. https://thissite.info?url='test.json'&dnsserver='https://thissite.info')如果变量未在 url 中定义,我还希望它默认为一个值。 For this, I'm using the following code:为此,我使用以下代码:

  function setup(){
  var dns = document.getElementById("dns");
  var urlbar = document.getElementById("urlbar");
  var frame = document.getElementById("viewport");
  if (typeof url === "undefined") {
    url = 'Welcome Page URL Here';
  } else {
    urlbar.value = url;
  }
    if (typeof dnsserver === "undefined") {
    dnsserver = 'Default Server Here';
  } else {
    dns.value = dnsserver;
  }
  var xmlhttp = new XMLHttpRequest();
       xmlhttp.onreadystatechange = function() {
         console.log(this.status)
         if(this.readyState == 4 && this.status == 200){
           var response = JSON.parse(this.responseText);
           frame.src=response;
           console.log("Request Completed with 200");
       };
         if(this.readyState == 4 && this.status == 404){
           console.log("Website Not Found");
           frame.src='404.html'
       };
       }
      xmlhttp.open("GET",dnsserver + url + '.json', true);
      xmlhttp.send();
      console.log("Request Sent");
}

But even if I pass the vars in the URL, it doesn't accept it.但即使我通过 URL 中的变量,它也不接受。

<script type="text/javascript"> $(function () { $("#btnQueryString").bind("click", function () { var url = "Page2.htm?name=" + encodeURIComponent($("#txtName").val()) + "&technology=" + encodeURIComponent($("#ddlTechnolgy").val()); window.location.href = url; }); }); </script> <input type="button" id="btnQueryString

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

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