How to store part of a url and when you click add to the rest of the link and open the page?

0

I am new to js and I hope for a solution.

I have several urls and they all start with link , now I want to store this as a first part and the rest in the links something like this:

Javascript=<br>
var **base**= "https: // 2.bp.blogspot.com" (algo como esto no se como se hace)
<br><br>
HTML=<br>
< a href="/-aVpKfrY4hCE/WUBqnQNNt-I/AAAAAAADLKU/iIMtGqmPe7o4EMo2ZlqEr0QR3qCC_-4MwCKgBGAs/s3200/SecW4rs_05_0157.webp">Imagen 01< /a><br>
< a href="/-3FIhHPOdz_w/WUBqnc-30NI/AAAAAAADLKU/8G-BHCDwyi4DHyhORNwLfXudSIVnKO2ugCKgBGAs/s3200/SecW4rs_05_0162.webp">Imagen 02< /a><br><br>
RESULTADO al hacer click=<br>
< a href="**base +**/-aVpKfrY4hCE/WUBqnQNNt-I/AAAAAAADLKU/iIMtGqmPe7o4EMo2ZlqEr0QR3qCC_-4MwCKgBGAs/s3200/SecW4rs_05_0157.webp">Imagen 01< /a><br>
< a href="**base +**/-3FIhHPOdz_w/WUBqnc-30NI/AAAAAAADLKU/8G-BHCDwyi4DHyhORNwLfXudSIVnKO2ugCKgBGAs/s3200/SecW4rs_05_0162.webp">Imagen 02< /a>
<br><br>

That is, by clicking open the url even though they are distentas but with the same first part stored.

    
asked by Manuel b 06.10.2017 в 14:19
source

2 answers

1

A href opens a link directly.

You can create a function in javascript and have it receive the second part from the HTML using onclick()

var base = "https://2.bp.blogspot.com";

function MiFuncion($segundaParte){      
        //Concatenar la primera parte "base" con la segunda parte $segundaParte
        var enlaceEntero = base.concat($segundaParte);
        //Abrir URL
        window.open(enlaceEntero,"_self")
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="MiFuncion('/segundaParteDelEnlace.com')">Haz click aquí para abrir el enlace</button>
<br>
<a onclick="MiFuncion('/segundaParteDelEnlace.com')">Haz click aquí para abrir el enlace</a>
    
answered by 06.10.2017 / 14:25
source
1

Without using javascript, and assuming that all the links on your page go to that site, you can add the following in the <head> of your page:

<base href="https://2.bp.blogspot.com">

and all relative links will use that address as a base

    
answered by 06.10.2017 в 15:19