简体   繁体   中英

How to insert value in a variable Json href attribute?

One question I wanted to put a variable inside href link not using code, which is in direct link creation.

The example is 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>

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

Note: I can not use scripts .

For inline your choice is really limited. For example you can use "onmouseover" event to replace placeholder with variable value on the fly.

Let's say you have following link

<a href="http://www.google.com/search?q=CODIGO_RASTREAMENTO" target="_blank">
  Search
</a>

In that case "CODIGO_RASTREAMENTO" is just a placeholder. At some point variable CODIGO_RASTREAMENTO gets assigned, eg

var CODIGO_RASTREAMENTO = 'stackoverflow';

You can modify the above link to

<a href="http://www.google.com/search?q=CODIGO_RASTREAMENTO"
   onmouseover="this.href = this.href.replace('CODIGO_RASTREAMENTO', CODIGO_RASTREAMENTO)" 
   target="_blank">
      Search
</a>

It replaces placeholder with a real value, so when you actually click the link - value takes effect.

Demo: http://jsfiddle.net/5v2S9/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM