0

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/

1
  • Your fiddle is set to run onLoad - change it to run not wrapped in <head> (so it is exposed globally) and change the name of the function
    – Ian
    May 21, 2013 at 17:03

1 Answer 1

9

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.

7
  • That's not the only problem - the fiddle runs onLoad, so whatever function they create won't be globally available (to the inline event handler)
    – Ian
    May 21, 2013 at 17:04
  • @Ian That's JSFiddle's problem (I had to change the settings of my fiddle to "No wrap - in <body>" May 21, 2013 at 17:05
  • That's the OP's/your problem, not jsFiddle's. If you're going to suggest a solution, you should mention all problems
    – Ian
    May 21, 2013 at 17:08
  • @Ian But I wouldn't think that is a problem - the JSFiddle is simply for demonstrating the code. I don't think the OP uses onLoad in his original code... I recognize his fiddle's settings are wrong, but is that really the problem? Thanks for you suggestion/comment though, appreciated. May 21, 2013 at 17:11
  • 1
    @AlfredXing I see what you're saying. I guess I just meant it wouldn't hurt to include the explanation of why it's now working in your demo - both the function name change and the fiddle setting. You're absolutely right that the OP (hopefully) wouldn't have the same problem in their own code due to the fiddle setting, but I would've explained anything I changed, whether it truly applies to them or not. Either way, it's probably unnecessary by now, and maybe even unnecessary in the first place :) Your answer's obviously right, I was just trying to explain more for the OP
    – Ian
    May 21, 2013 at 17:25

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.