Click on a link go to another page and show the desired id

Asked

Viewed 1,764 times

3

Well, I need to make sure that when clicking a link from one page go to another and only show the content of the id indicated on the first page, can you do this? I’ve searched a lot of places and I can’t find exactly what I’m looking for.

index php.

<a href="interna.php" onclick="toggle_visibility('bloco1');"></a>
<a href="interna.php" onclick="toggle_visibility('bloco2');"></a>

internal.php

<div id="bloco1" style="display:none">
<div id="bloco2" style="display:none">

This is the javascript I used

<script type="text/javascript">
<!--
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>

UPDATE Now the blocks are displayed but when I go to the second block all the functionalities of the links (for example back button) do not work, only the first block works perfectly. I noticed that in all links the code inserts internal.php? show=bloco1 for example, I don’t know if this is what’s troubling you now.

<a href="interna.php?exibir=bloco1"></a>
<a href="interna.php?exibir=bloco2"></a>

<script>

 function queryObj() {
    var result = {}, keyValuePairs = location.search.slice(1).split("&");
    keyValuePairs.forEach(function(keyValuePair) {
        keyValuePair = keyValuePair.split('=');
        result[decodeURIComponent(keyValuePair[0])] = decodeURIComponent(keyValuePair[1]) || '';
    });
    return result;
}

loads the content after everything is rendered

    $(document).ready(function () { 

calls the method to parse the url

    var objetoParaOcultar = queryObj();

shows div with id sent by parameter

    $('#' + objetoParaOcultar.exibir).show();
})
</script>
  • Send mode GET, buddy

  • Silvio Andorinha I didn’t understand what you meant, I’m not very good at javascript, I could explain?

1 answer

2

On its first page we will have the links on the page as below:

<a href="interna.php?exibir=bloco1">exibir 1</a>
<a href="interna.php?exibir=bloco2">exibir 2</a>

Already on your second page, we will have the following Javascript (remember to import jQuery before this code):

<script>

   function queryObj() {
      var result = {}, keyValuePairs = location.search.slice(1).split("&");
      keyValuePairs.forEach(function(keyValuePair) {
          keyValuePair = keyValuePair.split('=');
          result[decodeURIComponent(keyValuePair[0])] = decodeURIComponent(keyValuePair[1]) || '';
      });
      return result;
   }

   /*carrega o conteúdo apos tudo ser renderizado*/
   $(document).ready(function () { 

      /*chama o metodo para parsear a url*/
      var objetoParaOcultar = queryObj();

      /*mostra a div com o id enviado por parametro*/
      $('#' + objetoParaOcultar.exibir).show();

      /*Remove os parametro da url após mostrar a div*/
      window.location.href.replace(window.location.search,'');
   });

</script>

It works as follows: The function "parses" the parameter sent by its URL and then uses it to display the div.

I hope I’ve helped!

  • I didn’t quite understand what you did, but I’ll show you what I was doing in those hours after I posted it here. The last thing I tried to do was this <a href="internal.php? id=bloco1" onclick="Document.getElementById('bloco1').style.display='block';Return false;"> Only that also came to nothing, the short point is, that I just need to play a display:block in the referenced click id and show it. Can be in javascript or jquery itself

  • aeeeeeeeeeeeeeeeeeeeee very good, it worked now, I will have to study that part of Keyvaluepairs, search, Slice and split pq still do not understand very well their function. But I’ll see you, man

  • Giving a tested detected a problem when I go to lock2 all the functionalities of the links within it get lost, for example I have a back button and it does not work, but in lock1 works not know pq. The blocks are exactly the same only that only changes the content, for example any link gets more or less so internal.php? view=bloco1#cenario type when any link that is inside would not need to have internal.php:display=bloco1

  • Brunno - I’ll do better I’ll provide a link for you see what happens. The link is this here http://twinshark.com.br/statico/ the problem is that when you click on the second Thumb of the portfolio it shows the contents of block 2 only it loses all functionality of the other things inside block that has #

  • Brunno - First thanks for the elegio, I found out what happens now has the menu up there where has video direction, scenario concept and etc when I put the second block it only works the first by being on the same page know that this no longer has to do with the post but if you can help, why only work perfectly with a block the menu

  • Brunno - Was that same man thanks for everything now this perfect

  • Oops... please set the topic as settled... and big hug

Show 2 more comments

Browser other questions tagged

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