Get information from the url [duplicate]

1

Hello, I would like to know how I can get information through a URL like this below:

http://www.meusite.com/?primeiro=11111&segundo=222222&terceiro=3333333333

I wanted to get the information for the " first ", the " second " and the " first ". And maybe I do know, another one I could put, example " fourth ".

Oh and also wanted to identify which one I got, that is, type like this:

    
asked by anonymous 16.05.2016 / 13:52

2 answers

0

Oops! How are you? You can use PHP:

<?php 
$primeiro = $_GET['primeiro']
$segundo = $_GET['segundo']
?>

So, every time you refresh the page by sending the parameters, php will capture them. I do not understand much of PHP, so a cool stuff would be to look at w3schools: link There's a lot of stuff there. I hope in some way to have helped.

    
16.05.2016 / 13:59
0

First, I believe that your URL is malformed, you should start your queryString with the character ? , then we have the following URL:

http://www.meusite.com/?primeiro=11111&segundo=222222&terceiro=3333333333

Now let's go to Part 2, read this URL, so you can initialize a URL Object.

var urlAsString = "http://www.meusite.com/?primeiro=11111&segundo=222222&terceiro=3333333333";
var url = new URL(urlAsString);

If you make a console.log(json) , you will see that it has some properties that you can use, it follows the list of all the properties and their values:

hash: ""
host: "www.meusite.com"
hostname: "www.meusite.com"
href: "http://www.meusite.com/?primeiro=11111&segundo=222222&terceiro=3333333333"
origin: "http://www.meusite.com"
password: ""
pathname: "/"
port: ""
protocol: "http:"
search: "?primeiro=11111&segundo=222222&terceiro=3333333333"
username: ""

I do not even have to make use of url.search , you can even use the URLSearchParams " to read the parameters, but it is very recent and so it is not supported by many browsers, in this case I advise you to use the following code (with similar and comparable structure, although not implement all methods).

var URLSearch = function (search) {
  var that = this;
  this.entries = {};

  var start = search[0] == "?" ? 1 : 0;
  var params = search.substring(start).split("&");
  params.forEach(function (str) {
    var array = str.split("="); 
    var prop = array[0];
    var value = array[1];
    that.entries[prop] = value;
  });
}

URLSearch.prototype.get = function (prop) {
  return this.entries[prop];
}

URLSearch.prototype.set = function (prop, value) {
  this.entries[prop] = value;
}

URLSearch.prototype.has = function (prop, value) {
  return this.entries[prop] ? true : false;
}

URLSearch.prototype.delete = function (prop, value) {
  delete this.entries[prop];
}

URLSearch.prototype.toString = function (prop, value) {
  var params = [];
  for (var key in this.entries) {
    params.push(key + "=" + this.entries[key]);    
  } 
  console.log();
  return params.join("&");
}

var params = new URLSearch(url.search);

Now to a complete example:

var URLSearch = function (search) {
  var that = this;
  this.entries = {};
  
  var start = search[0] == "?" ? 1 : 0;
  var params = search.substring(start).split("&");
  params.forEach(function (str) {
    var array = str.split("="); 
    var prop = array[0];
    var value = array[1];
    that.entries[prop] = value;
  });
}

URLSearch.prototype.get = function (prop) {
  return this.entries[prop];
}

URLSearch.prototype.set = function (prop, value) {
  this.entries[prop] = value;
}

URLSearch.prototype.has = function (prop, value) {
  return this.entries[prop] ? true : false;
}

URLSearch.prototype.delete = function (prop, value) {
  delete this.entries[prop];
}

URLSearch.prototype.toString = function (prop, value) {
  var params = [];
  for (var key in this.entries) {
    params.push(key + "=" + this.entries[key]);    
  } 
  console.log();
  return params.join("&");
}

// para pegar a URL atual, utilize 'window.location.href'.
// var urlAsString = window.location.href;
var urlAsString = "http://www.meusite.com/?primeiro=11111&segundo=222222&terceiro=3333333333";
var url = new URL(urlAsString);

var params = new URLSearch(url.search);
console.log(params.entries);        // Object {primeiro: "11111", segundo: "222222", terceiro: "3333333333"}
console.log(params.get("segundo")); // 222222
console.log(params.toString());     // primeiro=11111&segundo=222222&terceiro=3333333333

params.set("quarto", 44444);
console.log(params.toString());     // primeiro=11111&segundo=222222&terceiro=3333333333&quarto=44444

params.delete("terceiro");
console.log(params.toString());     // primeiro=11111&segundo=222222&quarto=44444

BONUS

I really do not know what you're trying to do with the values in the queryString, but if you're thinking of creating a Route, I'd advise looking at the following library:

link

    
16.05.2016 / 14:51