简体   繁体   中英

Simple javascript function not working in IE7

Code works across all major browsers, but firing a simple alert on click is not working.

This is in my header

<script type="text/javascript">
    function this_function() {
        alert("got here mango!");
    }
</script>

This is in the body

<button type="button" onclick="this_function()">click me</button>

If I put the "onclick" into the tag then it works fine and dandy.

Any and all suggestions on how to get this to work in IE would be great. Thanks in advance.

Sorry, by "into the tag" i meant putting onclick="alert()" into the tag.

Try: <button type="button" onclick="javascript:this_function();">click me</button>

It's advised to separate JavaScript and markup. Thus regardless you should assign an ID to the button and attach the onclick handler like this:

document.getElementById("button").onclick = function() {
     alert("got here mango!");
};
​

Are you running this sandboxed? If you aren't I would highly suggest trying this all by its self in a single HTML file with no other things going on. It is possible that IE7 is blowing up (quietly) on another script issue that is preventing your "this_function" from loading into the DOM properly.

After you have done this put the in your there is no need for it to be in the head and I have actually seen this cause problems under certain conditions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM