Span tag onclick loads blank page?

Issue

I’ve got a span tag:

<span id="myBtnfb"
    class="demo" onclick="open()">
    <span class="icon">&nbsp;</span>
    <span class="">Testing</span>
</span> 

and some javascript

  function open() {
      alert('Hello');
  }

Now, what I want to happen is when the span is clicked, it fires the function “open”. What seems to happen though is it just reloads the page but brings back a blank page. Any ideas?

http://jsfiddle.net/qtf5s/

Solution

open() is a native JavaScript function, which opens a new page (in this case blank since there’s no params specified): https://developer.mozilla.org/en-US/docs/Web/API/Window/open

Just rename your function to something like openAlert(): http://jsfiddle.net/qtf5s/2/.

Also, @Ian mentioned in the comments that in JSFiddle, it’s best to change the JavaScript wrap to "No wrap – in " so that the code works.

Answered By – Alfred Xing

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published