-3

I have some simple code to open reddit, but i need it to open in a new window. tried some different approaches but can't get any to work. Any idea what to add to below code?

<a href="http://www.reddit.com/submit" onclick="window.location = 'http://www.reddit.com/submit?url=' + encodeURIComponent(window.location); return false"><img src="http://www.reddit.com/static/spreddit7.gif" alt="submit to reddit" border="0" /></a>
legoscia
  • 39,593
  • 22
  • 116
  • 167
lowercase
  • 1,198
  • 9
  • 34
  • 56

2 Answers2

0

Try this

window.open("http://www.reddit.com/submit?url="+encodeURIComponent(window.location))

Check this link for further information

Fritz
  • 9,987
  • 4
  • 30
  • 49
PSR
  • 39,804
  • 41
  • 111
  • 151
0

You obviously want to suppress the standard link behaviour by adding

return false;

to the onclick handler.

<a href="#" onclick="window.open('url');return false;">label</a>

Consider putting that logic in a separate click handling function

function handleClick(evt) {
   var url = '' // build your url here
   window.open(url);
   evt.target.preventDefault();
}

And add it to your link with

element.addEventListener('click', handleClick);
line-o
  • 1,885
  • 3
  • 16
  • 33
  • there has to be an easier way without altering/adding all this though? – lowercase Mar 15 '13 at 14:26
  • Honestly, lowercase, you can't make the old proverbial omelette without breaking a few eggs. Perhaps it would be best to have a bit of a read up on JavaScript to see what it can do. – Paul Mar 15 '13 at 14:38
  • I'm not sure the point of this site is for me to go away and read up on javascript. If only I had the time. Thanks for your efforts though. – lowercase Mar 15 '13 at 14:51