-1

I'm having an issue making a "random" link open up in a new tab.

      var randomLink = function(){

  var links = [
    'youtube.com/channel/UCkq10sWOK1ftebGZPFJBekA',
    'soundcloud.com/iamcardib',
    'soundcloud.com/rickybobby-slumpgod',
    'soundcloud.com/wavey-hefner',
    'soundcloud.com/liluzivert',
    'soundcloud.com/flameemojimusic',
    'soundcloud.com/youngthugworld',
    'soundcloud.com/kodak-black'

  ];


  var max = (links.length)

 window.open=links[Math.floor(Math.random()*max]

  var link = links[randomNumber];
  window.location = "http://" + link;

};

I've multiple times to get the links to open up in a new tab when I click a button but it will only open in the same tab.

jaystang
  • 9
  • 1
  • `window.open=links[Math.floor(Math.random()*max]` I think you are missing a close bracket on this line – James Douglas Aug 25 '17 at 18:17
  • 1
    @Amy — Not a duplicate of that. That is about a link opening in a new tab and the answers focus on using plain HTML rather then using JS as you need for a random URL like this question is asking about. – Quentin Aug 25 '17 at 18:19
  • @Quentin - JavaScript answer is also present in the suggested duplicate - https://stackoverflow.com/a/33546457/477420. So far it is very unclear what you are having trouble with (short of some strange `window.open=links...` syntax) – Alexei Levenkov Aug 26 '17 at 03:42
  • 1
    @AlexeiLevenkov — I am not jaystang. – Quentin Aug 26 '17 at 09:24

3 Answers3

1

window.open is a function. You need to call it, not assign a new value to it.

You also have a syntax error. A ) is missing.

window.open(links[Math.floor(Math.random()*max)]);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

The syntax of window.open is window.open(stuff) not window.open=stuff, because window.open is a function.

As @Quentin said,

window.open is a function. You need to call it, not assign a new value to it.

Functions are called like this: functionName(parameters);

Values are assigned like this: variableName=value;

Also, you are missing a close bracket and a semicolon. If you apply these,

window.open=links[Math.floor(Math.random()*max]

becomes

window.open(links[Math.floor(Math.random()*max)]);
James Douglas
  • 3,328
  • 2
  • 22
  • 43
-3

I think it should be window.open=links[Math.floor(Math.random()*max)]