E
E
Endru2016-12-07 23:02:18
JavaScript
Endru, 2016-12-07 23:02:18

js. Variable inside href?

Is it possible to somehow put a variable inside href?

var urlGo=http://goo.gl;
var googleLink = window.document.querySelectorAll("a[href^='urlGo']");

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Viktor Yanyshev, 2016-12-07
@villiwalla

var link = document.querySelectorAll('a[href]');
for ($i = 0; link.length > i; i++) {
 link[i].setAttribute('href', 'http://goo.gl');
}

A
Alexander Pozharsky, 2016-12-08
@alex4321

Pure JS - only in the script, not in the template (see Viktor's and ErgonomicCode's answers).
If a lot of such good is planned, I would look towards template engines

A
Anton fon Faust, 2016-12-15
@bubandos

First you need to fix the errors in the code)))
And only then ...

var urlGo="http://goo.gl"; // строки надо в кавычки брать
// можно так
var googleLink = window.document.querySelectorAll("a[href^="+urlGo+"]");

// или вот так
var selector = [
    "a[href^=", 
    urlGo, 
    "]"
].join('');
var googleLink = window.document.querySelectorAll(selector);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question