-1

Im having issues with this javascript running prior to the user input, can someone help me fix this.

im just trying to make a little html page with a textbox and a button, that then clicked opens a new windows with a modified URL.

<input type="text" name="enter" class="enter" value="" id="lolz" />
<button type="button" id="the_button">Count</button>

document.getElementById('open').addEventListener('click', myFunction());
function myFunction() {
  var button = document.getElementById("the_button");
  var siteid = document.getElementById('lolz').value
  button.onclick = count();

  function count() {
    window.location = "http://www.websiteimusing.com/" + siteid;
  }
 }

You can check the code out here The Actually generate output from that website's code is this

Updated Code

document.getElementById('the_button').addEventListener('click', myFunction);
 function myFunction() {
   var button = document.getElementById("the_button");
   var siteid = document.getElementById('lolz').value
    button.onclick = count();

 function count() {
     window.location = "http://www.websiteimusing.com/" + siteid;
   }
  }
4

3 回答 3

2

改成:

document.getElementById('open').addEventListener('click', myFunction);

当你放在()函数名后面时,表示当时调用该函数。

于 2013-07-16T04:36:52.723 回答
2

您的代码中没有带有 id 的元素open,因此您尝试将事件侦听器添加到 null。控制台会告诉你同样的信息:

Uncaught TypeError: Cannot call method 'addEventListener' of null

正如其他海报所说,还要确保从您的事件侦听器函数中删除括号。

于 2013-07-16T04:41:04.907 回答
1

在这条线上

document.getElementById('open').addEventListener('click', myFunction());

您通过添加()

将其更改为:

document.getElementById('open').addEventListener('click', myFunction);
于 2013-07-16T04:37:09.943 回答