Pass QueryString from one page to another using JavaScript

jo15765
 
on Jul 05, 2022 11:36 PM
Sample_892931.zip
353 Views

I am new to JavaScript and need an example of how to pass an int variable value using url query params in a fetch request to a separate page. 

This page is making an API request with the variable, and then returns the results of the API back to the first page (calling) page.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 06, 2022 06:44 AM

Hi jo15765,

Refer below example.

HTML

Default

<input type="text" id="txtId" />
<input type="button" value="Send" onclick="SendData()" />
<hr />
<span id="lblId" />
<script type="text/javascript">
    function SendData() {
        var id = document.getElementById("txtId").value;
        var popup = window.open("Popup.aspx?Id=" + id, "Popup", "width=300, height=100");
        popup.focus();
    }

    function GetData(data) {
        document.getElementById("lblId").innerHTML = data;
    }
</script>

Popup

<script type="text/javascript">
    if (window.opener != null && !window.opener.closed) {
        var id = location.search.split('=')[1];
        // Get data from API based on the Id.
        window.opener.GetData(parseInt(id) + 1);
        window.close();
    }
</script>

Screenshot