As I create an "a" element from a string and execute the url

0

I always get links similar to this, I need to execute the link to execute the redirection.

I thought I would do it with location.href

location.href ="http://xxx.com/index.php?x=xx";

But it would require extracting only the link and I do not think it can be done since the links vary.

let enlace = ' <a  class="btn" href=http://xxx.com/index.php?x=xx>Ir al enlace</a>';
console.log(enlace);

I thought I would do it with RegExp but I do not think it can be because of the variation of the characters.

The last possibility that occurs to me is to create the element from that string and execute that link for the resend, I think it is the most viable.

    
asked by Pablo Contreras 14.09.2017 в 11:37
source

1 answer

1

You can create the element with jQuery and retrieve the href attribute:

var enlace = $(' <a  class="btn" href=http://xxx.com/index.php?x=xx>Ir al enlace</a>');
console.log(enlace.attr('href'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 14.09.2017 / 11:41
source