Concatenate a link inside a href attribute with a variable?

2

I would like to know how do I concatenate a link within a href attribute with a variable? Note must be something 'inline' . I use a platform that does not let me use scripts inside the page so the inline reason.

Example:

var teste = [x];
<a href="#" onclick="window.open('http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_COD_UNI=',teste)" target"_blank">vai</a>

The output should be:

http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_COD_UNI=[x]

My code is wrong in organizing the elements or quotes in the wrong place so disregard this part. I need a hint of how I would ride this.

    
asked by anonymous 09.05.2014 / 16:47

3 answers

7

Define a global variable and use the + operator to concatenate:

<script>
var teste = "blabla";
</script>

<a href="#" onclick="window.open('http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_COD_UNI=' + teste)" target"_blank">vai</a>

Demonstration

The concatenation will occur at the time of the click.

    
09.05.2014 / 16:49
2

You can create an input hidden and set a value something like:

<input type="hidden" name="teste" id="name" value="qualquer-valor"/>

Then you get the value and concatenate your link something like:

onclick="window.open('http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_COD_UNI=' + document.getElementById('teste').value)"
    
09.05.2014 / 16:52
1

You can use "eval", but its use is not recommended for security reasons.

So:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var teste = 'blah';
    </script>
    <a href="#" onclick="eval('window.open(\'http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_COD_UNI=\' + teste)')" target"_blank">vai</a>
</body>
</html>

Beware of using eval. As I said, it does have security holes, but since you said that the code has to be inline, that solves it.

    
09.05.2014 / 16:53