Show variable in script src

Asked

Viewed 475 times

2

I need to pass two parameters via url and create a JS function to receive these parameters and place them at the source of a script.

Follow what I got.

Link to the parameters: <a href="<?php bloginfo( 'url' ); ?>/cotacao?id=17&fonte=Verdana">Dólar</a>

Function to pick the parameters:

<script>
                    var url   = window.location.search.replace("?", "");
                    var items = url.split("&");
                    var array = {
                      'id' : items[0],
                      'fonte' : items[1]
                    }
                    var id = array.id;
                    var fonte = array.fonte;
                    //alert(id);
                    //alert(fonte);
                </script>

Script where the id and source variables should come:

<!-- Widgets Notícias Agrícolas - www.noticiasagricolas.com.br/widgets -->
 <script type="text/javascript" src="http://www.noticiasagricolas.com.br/widget/cotacoes.js.php?ID&FONTE&tamanho=10pt&largura=500px&cortexto=333333&corcabecalho=B2C3C6&corlinha=DCE7E9&imagem=true&output=js"></script>

Error returned:

inserir a descrição da imagem aqui

Image link: image

  • 3

    It would not be better if you declared these variables as global or put in a configuration object?

  • Hello André, the way I did I can get the values, but I can not insert the variables in the script src. The result of the Alerts that are commented are: Alert(id) -> id=17 and Alert(source) -> source=Verdana... exactly what I need to put in the ID and FONTS places in src. Sorry I’m new to programming.

  • I get it. You has do this with javascript or you can use PHP to print these values in src?

  • @When you solve a question you created, create an answer and mark it as accepted. Here we do not put solved in the question nor the solution. It is a site of questions and answers. Thank goodness that solved your problem :)

  • 1

    Hi Jorge, thanks for the feedback.

1 answer

2


Solution: I swapped the entire function for an echo, adding $_GET where I wish.

 <?php echo "<script type='text/javascript' src='http://www.noticiasagricolas.com.br/widget/cotacoes.js.php?id=".$_GET['id']‌​."&tamanho=12pt&cortexto=333333&corcabecalho=B2C3C6&corlinha=DCE7E9&imagem=true&o‌​utput=js'></scrit>"; ?>

Browser other questions tagged

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