简体   繁体   中英

How to get URL parameters from custom input with JQuery

I have a view that I want to store some URL parameters and have tried everything. Here is my current code.

Sample URL: https://www.facebook.com?username= ""&password=""(I'm trying to collect the parameter values)

HTML:

<input id="urlInput" type="text" placeholder="Enter URL" class="form-control" />

Javascript:

var url = $("#urlInput").val();//This pulls the value of the input/URL with parameters
var getURLUser = url.substring(url.indexOf("?"));
$("#urluser").html(getURLUser);

What is wrong? Thanks.

Currently, you're getting a String from the beginning of ? to the end of the String.

You could split the string by &password= , and get the right side of it:

var url = "https://www.facebook.com?username=123&password=(I'm trying to collect the parameter values)";
var getURLUser = url.split("&password=")[1];
// results in "(I'm trying to collect the parameter values)"
console.log(getURLUser);

or

var url = "https://www.facebook.com?username=123&password=(I'm trying to collect the parameter values)";
var getURLUser = url.split("&password=");
console.log(getURLUser[0]);
// results in "https://www.facebook.com?username=123"
console.log(getURLUser[1]);
// results in "(I'm trying to collect the parameter values)"

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