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.

Mathew Dodson
  • 115
  • 1
  • 1
  • 10

1 Answers1

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)"
Leo
  • 674
  • 5
  • 18
dustytrash
  • 1,568
  • 1
  • 10
  • 17
  • 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. – Mathew Dodson Sep 11 '18 at 17:47
  • 1
    @MathewDodson `url.split('?username=')[1].split('&password')[0];` – dustytrash Sep 11 '18 at 17:49
  • Works like a charm. Thank you. – Mathew Dodson Sep 11 '18 at 17:55