1

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.

2

1 Answer 1

3

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)"
Sign up to request clarification or add additional context in comments.

3 Comments

I've tried adjusting the numbers, but to no avail. How would you get just the username in the middle? Right now, I have "var geturlUser = url.split("?username=")[1] && url.split("&password=")[0];"//After the username and before the password?? Same Example applies.
@MathewDodson url.split('?username=')[1].split('&password')[0];
Works like a charm. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.