-1

So I have a page in Google sites with the following code inside an HTML Box:

<a id="resource-link" href="#">My Link</a>

<script type="text/javascript">
   document.getElementById("resource-link").addEventListener("click", function(e){
      e.preventDefault()
      window.open("http://www.google.com","_blank");
   });
</script>

I have also tried using the onclick attribute rather than addEventListener()

Works fine in this JSFiddle.

When this code is put into an HTML Box, clicking the link results in opening a new tab (what I want), however before loading http://www.google.com, it is redirected to the url of the original page with the link on it.

P.S. I really hate Google Sites and all its nonsense.

UPDATE: the reason I am not using a simple <a href="..."></a> is because I need to open multiple tabs with a single click. The single call to window.open(google) is just an example.

UPDATE 2:

changing the code to:

<a onClick="open_resources()">My Link</a>

<script type="text/javascript">
  var open_resources = function(){
    window.open("http://www.google.com","_blank");
  };
</script>

results in the following error:

Uncaught script error: Uncaught TypeError: Expected property "open" to be a function, not undefined: undefined in source: "<click handler>" at line: -1
thedarklord47
  • 3,183
  • 3
  • 26
  • 55

2 Answers2

0

You could change the element from an anchor to a span and style it to look like a link:

head

<style>
    #resource-link{
        color: blue;
        cursor: pointer;
        text-decoration: underline;
    }
</style>

body

<span id="resource-link">My Link</span>

<script type="text/javascript">
  document.getElementById("resource-link").addEventListener("click", function(e){
     window.open("http://www.google.com","_blank");
  });
</script>
mpallansch
  • 1,174
  • 6
  • 16
0

Another answer could be to have the first link in the anchor tag and other ones in the click handler, like this:

<a id="resource-link" href="http://www.site1.com" target="_blank">My Link</a>

<script type="text/javascript">
   document.getElementById("resource-link").addEventListener("click", function(e){
      window.open("http://www.site2.com","_blank");
      window.open("http://www.site3.com","_blank");
      // ...
   });
</script>
mpallansch
  • 1,174
  • 6
  • 16
  • properly opens site1, but click handler is still not called. – thedarklord47 Jun 18 '15 at 19:22
  • are you sure something in google sites isn't preventing your javascript or your click handler? have you added alert/log statements to see if it reaches them? – mpallansch Jun 18 '15 at 19:25
  • I'm sure the problem is something I don't understand about Google Sites. If you look at my update, I have another piece of code that should do the same thing. Neither are successfully calling my click handler. Are you aware of any restrictions on event listeners or the `onClick` attribute in Google Sites? I could not find any in my searches – thedarklord47 Jun 18 '15 at 19:41
  • not that I'm aware of. are scripts working at all (put an alert statment outside the click hanlder)? and are you sure javascript is enabled on your browser? – mpallansch Jun 18 '15 at 19:48
  • Javascript is enabled on my browser. Is there something I need to do to make JS work on Sites? I get the following when putting `alert("Hello!");` within the script tags: `Uncaught script error: "alert" is not defined in this scope. in source: "unknown" at line: 0` – thedarklord47 Jun 18 '15 at 19:56