Opening HTML inside a DIV does not recover the parameters via GET

Asked

Viewed 1,943 times

1

I am using Jquery to load an HTML page into a <div>. My code is working correctly, but it’s not passing a value via GET that I need on the other page:

<!DOCTYPE html>
<html>
<head>
<title>Exemplo</title>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    
<script type="text/javascript" src="js/js_1.9/jquery-1.8.2.js"></script>  
<script type="text/javascript" src="js/js_1.9/jquery-ui-1.9.1.custom.min.js"></script>  
</head>
<body>    
 <div id="sidebar">
    <ul>
        <li><a onclick="carregar('receita.html?cod=1')" href="#">Receita 1</a></li>
        <li><a onclick="carregar('receita.html?cod=2')" href="#">Receita 2</a></li>
    </ul>
</div>
<div id="conteudo"></div>
</body>
<script>
    function carregar(pagina){
        $("#conteudo").load(pagina);
    }
</script>
</html>

I’m using a page function receita.html that recovers the variable cod

So if I open in the browser receita.html?cod=10 it recovers this variable, but opening the content within the <div> he even opens the page, but does not recover the cod.

Does anyone have any idea how I can fix this?

  • javascript is correct, the problem seems to be in receita.html, can you add the part of that file where you use the GET variable? Try adding var_dump($_GET);exit; at the beginning of the file to see what appears

  • I did it but nothing happened, I am recovering like this: function getUrlVars(){&#xA; var vars = [], hash;&#xA; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');&#xA; for(var i = 0; i < hashes.length; i++)&#xA; {&#xA; hash = hashes[i].split('=');&#xA; vars.push(hash[0]);&#xA; vars[hash[0]] = hash[1];&#xA; }&#xA; return vars;&#xA;}&#xA;var var_cod = getUrlVars()["cod"]; If I open the recipe.html page in my browser it works... only inside the other div

  • identified the problem, you are searching the GET variables with javascript in receita.html, but when you call the page with .load('receita.html'), the javascript of the page are not executed

  • That’s right... and I don’t know how to load them. hidden and at the time that load this page pull the values of these fields

1 answer

1

I realized you’re trying to get a value you already have (cod is in the attribute onclick). These values can be accessed in javascript through the attributes data-* where * may be replaced by a.

Since you are already using jQuery, it is better to use the function .on('click', ...) than the attribute onclick.

jQuery has the function .data() that accesses attribute values data-* directly.

$('#sidebar a').on('click', function() {
  // this se refere ao <a> clicado
  var cod = $(this).data('cod'); // acesse sem o prefixo "data-"
  $("#conteudo").html('"cod" é igual a ' + cod);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="sidebar">
  <a data-cod="1" href="#">Receita 1</a><!-- observe data-cod="1" -->
  <a data-cod="2" href="#">Receita 2</a>
</div>
<div id="conteudo"></div>

Browser other questions tagged

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