【问题标题】:How can I pass a value in a URL and insert value in a new URL to redirect with Javascript?如何在 URL 中传递值并在新 URL 中插入值以使用 Javascript 重定向?
【发布时间】:2023-03-14 03:31:01
【问题描述】:

我正在以http://example.com/page?id=012345 的形式在URL 中传递一个值。然后需要将传递的值插入新 URL 并将页面重定向到新 URL。这是我一直在使用的东西

function Send() {
    var efin = document.getElementById("id").value;
    var url = "https://sub" + encodeURIComponent(efin) + ".example.com" ;
    window.location.href = url;
};    

【问题讨论】:

    标签: javascript url redirect parameters


    【解决方案1】:

    听起来您正在寻找 URLSearchParams 的功能 - 专门使用 .get() 从 URL 中获取特定参数

    // Replacing the use of 'window.location.href', for this demo
    let windowLocationHref = 'http://example.com/page?id=012345';
    
    function Send() {
        let url = new URL(windowLocationHref);
        let param = url.searchParams.get('id');
    
        let newUrl = "https://sub" + encodeURIComponent(param) + ".example.com" ;
        
        console.log('Navigate to: ' + newUrl);    
        //window.location.href = newUrl;
    };
    
    Send();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2015-10-26
      • 2019-03-24
      • 1970-01-01
      相关资源
      最近更新 更多