Get information from the url

Asked

Viewed 715 times

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 from "first", of "according to" and of "weaver". And maybe I don’t know, another one I could put, example "quarter".

Ah and also wanted to identify which one I took, I mean, like this:

um alert como exemplo

  • 5

    Would that be something like? http://answall.com/questions/424/como-fa%C3%A7o-para-ler-values-da-url-in-javascript-querystring

2 answers

0

Whoa! 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 don’t know much about PHP, so a cool material would be looking at w3schools: http://www.w3schools.com/php/php_forms.asp There’s a lot of that going around. I hope somehow I’ve helped.

  • I’m sorry, but you don’t have the strength.

  • :( &#I am learning the way of the (dark) side of the force. So I can finish what my grandfather started.

0

First, I believe your URL is misshapen, 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 the second part, read this URL, for such 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, follows the list of all 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: ""

need not even say that you will need to make use of the url.search, you can even make use of the API URLSearchParams to read the parameters, however it is very recent and therefore not supported by many browsers, in this case I advise you to use the following code (with similar structure and comparable, 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 full 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 don’t know what you are trying to do with the values of queryString, but if you are thinking of creating a Route, I advise you to look at the following blibioteca:

https://github.com/chrisdavies/rlite

Browser other questions tagged

You are not signed in. Login or sign up in order to post.