How to insert Json variable value into a href attribute?

Asked

Viewed 1,287 times

-1

A question I wanted to put a variable inside the link href without using code, which is direct in creating the link.

The example is just below:

<a href="http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_COD_UNI=" + [CODIGO_RASTREAMENTO] targert="_blank">código de rastreamento</a>

the variable [CODIGO_RASTREAMENTO] comes from a file Json that is on the server side.

Note: I cannot use scripts.

  • 1

    I don’t really understand where the variable... of a JSON? on the server side comes from? Of the ways qq the closing quote is misplaced. Explain it better and we can help more.

  • How is the JSON file? Are you reading it? How is this variable?

  • doesn’t matter much where this json file comes from, I just wanted to know the correct placement of the quotes inside href.

  • I use a platform called vtex, the json file is generated directly from them and I don’t have access to what comes to min and this json variable [CODIGO_RASTREAMENTO] and just need to insert it in the link.

  • Then you get the client-side or server-side JSON??

  • @Dann I recommend that you change the question title to How to insert Json variable value into a href attribute?

  • 1

    @Curious Leandrocurious, for me the question is not yet clear. If the JSON is received on the server side the answer is one, if it is on the client side the answer is another...

  • ok @leandrocurious I’ll do that thank you so much for the info.

  • @Sergio agree! Also specify if it’s a server side or client side.

  • 1

    Okay, so JSON is on the server side.. what language do you have? PHP, Node.JS, ASP ?

  • 1

    I think the author has cooperated to clarify the question I think should not be closed. This comment will be deleted.

  • Dann: What server-side language do you have? PHP? can you put the function that fetches JSON?

  • @Sergio I honestly do not know what language the staff uses I only have access to frontend this platform VTEX that I use has some restrictions but it is possible to do.

  • Okay, and where does this signal code come from + ? ...INGUA=001&P_COD_UNI=" + [CODIGO_RASTREAMENTO] . I would say that the + should disappear and the [CODIGO_RASTREAMENTO] should be before the quotation marks end. But not knowing which language is hard.

  • that part of the code I cannot change to '<a href="http://websro.correios.com.br/sro_bin/txect01$. Querylist? P_LINGUA=001&P_COD_UNI=' comes direct from the site (www.correios.com.br) and this [CODIGO_RASTREAMENTO] I would need to concatenate with the link above.

  • because the question is this, if the link is static content you need to know which language to concatenate. Each language has its symbol to concatenate... Can you put a link to this VTEX? I don’t know what it is, maybe there is documentation there.

  • @Dann, when you’re around, jump into this chat to try and understand the question and maybe help: http://chat.stackexchange.com/rooms/14407/vtext

  • I did it myself but I tried to write differently.

Show 13 more comments

1 answer

1

You need to parse the json string to turn it into an object variable.

//JQuery
var objJson = $.parseJSON("string json");

//Javascript
var objJson = JSON.parse("string json");

So you could use the object variable objJson and call its attributes.

Example in jQuery of how to put href

$("#seu-link").attr("href",objJson.CODIGO_RASTREAMENTO);

Where $("#your-link") selects the tag by the id, it could be by class also $(".your-link"), as I saw in your example above, your link is in a recursion so it’s best to assign a class.

Example javascript for how to put href

document.getElementById("seu-link").href = objJson.CODIGO_RASTREAMENTO;
  • 1

    thanks for the answer but that’s not what I really needed I can’t use scripts, I had to do it within a href

  • If the code solved your problem be sure to mark as answer.

  • Apparently JSON is caught on the server side...

  • You treat this link client or server side?

  • 3

    Leandro, he edited the question and added "it comes from a Json file that is on the server side."

  • 1

    Good if it is server side which language you use? Because I understand that json comes from an external file but this link is created in the back-end?

Show 1 more comment

Browser other questions tagged

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