简体   繁体   中英

How do I change a parameter in a URL upon submitting in JavaScript

I want the following URL to be manipulated and opened in a new tab once I pass the value of email ID and click submit. The searchText in myUrl is the param for email ID:

HTML:

<textarea name="email" id="email" cols="30" rows="10"></textarea>
<button id="myButton">submit</button>

JavaScript:

const myUrl = new URL('something.com?searchText=weijsdo@hotmail.com&site=something&mobileVersion=&locale=&forceAaaApplication=');

you can use this:

HTML:

<button id="myButton" onclick="sbButtonClick()">submit</button>

JavaScript:

const textarea = document.getElementById("email");
let myUrl = null;

function sbButtonClick() {
    myUrl = new URL(`something.com?searchText=${textarea.value}&site=something&mobileVersion=&locale=&forceAaaApplication=`);

    // do something with myUrl
}

Edit

to get multiple email addresses one of the easiest ways is this way:

const textarea = document.getElementById("email");

function sbButtonClick() {
    // split the text by line breaks so each address should be in a separate line
    const emailArray = textarea.value.split("\n");

    // here you open tab for each email address
    for (let address of emailArray) {
        window.open(`something.com?searchText=${address}&site=something&mobileVersion=&locale=&forceAaaApplication=`);
    }
}

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