Get URL ID Code and insert into JS

0

How to get the code galeria of url http://URL/galerias.php?galeria=casais and put in js?

$(document).ready(function() {
    $('#conteudo').load('galeria.php?galeria='prodId'', 
      function(){
        $('#gallery a').lightBox();
      }
    );
});
    
asked by anonymous 04.03.2015 / 04:28

3 answers

1

When you have a URL of this type:

http://URL/galerias.php?galeria=casais

the ?galeria=casais part is called query string and you can fetch it with window.location.search " in string format. Now you just need to remove that info, for example via regex. If the key value you are looking for is galeria you can do this:

$(document).ready(function() {
    var query = window.location.search;
    var match = window.location.search.match(/galeria=(\w+)/);
    var galeria = match && match[1];
    $('#conteudo').load('galeria.php?galeria=' + prodId, 
      function(){
        $('#gallery a').lightBox();
      }
    );
});

The reason I first do match and then galeria = match && match[1]; is because in case there is no match avoid giving access error match[1] , because if there is match method will give null and not array .

    
04.03.2015 / 08:22
0

You can recover by php

$galeria = $_GET['galeria'];

Then create an input of type hidden in html

<input type="hidden" id="galeria" value="<?php echo $galeria; ?>">

and finally retrieve it in javascript

var proId = $('#galeria').val();

It's a way I thought at first, I think there might be something better.

obs: I did not get to test

    
04.03.2015 / 05:10
0

Dude, the way you're doing all this seems pretty complicated, why do not you try to simplify?

To know which gallery was selected you could do something like this:

<ol>
    <li class="galeria" data-galeria="casais">Casais</li>
    <li class="galeria" data-galeria="gravidas">Gr&aacute;vidas</li>
    <li class="galeria" data-galeria="criancas">Crian&ccedil;as</li>
    <li class="galeria" data-galeria="bebes">Beb&ecirc;s</li>
</ol>

And to load the galleries that are being clicked you do the following:

$(document).on("click", ".galeria", function()
{
    var galeriaSelecionada = $(this).data("galeria");

    $('#conteudo').load('galeria.php?galeria=' + galeriaSelecionada, 
      function(){
        $('#gallery a').lightBox();
      }
    );
});
    
04.03.2015 / 16:05