Get URL Code ID and insert into JS

Asked

Viewed 983 times

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();
      }
    );
});
  • 2

    I don’t understand what your code example has to do with the question: if you already have a value for prodId, just use it. But if you don’t have it, and want to recover from URL current, then why are you gonna make a call with load to the same URL? Please explain better what your question is.

  • @mgibsonbr of a check in my site http://rafaelaalves.com/pjax/ when I enter the gallery it lists all the galleries, but when I select some it opens the same but when I click to open a photo via lightbox it opens the image normally, and able to put the received URL code in the prodId I can make a second page that makes this function appear

  • I still don’t understand... Anyway, you’ve tried using window.location.search? Look for galeria= in this text, soon after will be its value (followed by a &, of a # or the end of the string). P.S. I opened your site in Chrome, and if I click a gallery, it goes to the right gallery...

3 answers

1

When you have a URL of this type:

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

the part ?galeria=casais is called query string and you can pick up with window.location.search in format string. Now you just need to get that info out of there, for example via regex. If the key of value what you seek is galeria you can do it like 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 do it first match and then galeria = match && match[1]; is because in case there is no match avoid giving error while accessing match[1], because if there isn’t match the method will give null and not a array.

  • Hello Sergio sorry I didn’t explain correctly but I would like the query string ? gallery=couples the value couples were in the place of 'prodId', because it can contain the value ? gallery=couples | ? gallery=children | ? gallery=seniors and so on

  • @Cristianocardososilva so you want to redirect the page? In that case enough: window.location.search = window.location.search.replace(/galeria=(\w+)/, prodId);

  • not well redirect, but rather open it within a DIV

  • @Cristianocardososilva to see if I understand then: This value ? gallery=couples | ? gallery=children | ? gallery=seniors come from where? and when you have it you want to do .load() with the right url? (note that in my reply there was an error copied from your code, where a + to concatenate the variable here: .load('galeria.php?galeria='prodId'' (in the answer is already right now).

  • not well redirect, but rather open the contents inside a DIV type I have the following DIV <div id="content"></div> when I open the page '/gallery/galleries.php? gallery=gravidas' wish that within the content div she carries to me the page '/gallery/gallery.php? gallery=gravidas , so when accessing the page '/gallery/galleries.php? galeria=gravidas' the first photo I open it loads right into the URL '/gallery/gravidas/0IMG_5260.jpg' and it was to open in a lightbox but when you go to the browser and send back already works lightbox normally

  • take a look (http://rafaelaalves.com/galeria)

Show 1 more comment

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 recover in javascript

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

It’s a way I thought at first, I believe there can be something better.

BS.: I didn’t get to test

  • I did not understand what you wanted to do with that javascript code that you put, anyway I drafted an answer according to the title of the question.

0

Man, the way you’re doing all this seems really complicated to me because you don’t 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();
      }
    );
});

Browser other questions tagged

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