简体   繁体   中英

javascript browser problem works in FF but not in IE and Chrome

<div id = '147'> 
    <u><b><font color = 'blue'  onClick = 'javascript:showbox(147)'>Comment </font</b></u>
</div>

<script type='text/javascript'>
<!--        
    function showbox(var1) {
        document.getElementById(var1).innerHTML = var1 + " <form name = 'commentform'  method = 'post' action = ''><input type='text' name = 'comment' value='Enter Your Comment' ><input type='hidden' value= " + var1 + " name='wallpostid'> <input type ='hidden' value = '$userid' name = 'userid' > <input type='submit' name = 'send' value='Post your Comment' onClick='javascript:postcomment()'>  </form>";
        <b onclick= 'postcomment()' > </b>;
    }
    function postcomment() {
        document.commentform.comment.submit();
    }
-->
</script>

Above is the code. It is working in FF but not in IE or Chrome.

Can anyone tell where I went wrong. Any solution to make it work in all browsers?

There are some typos here is one:

"...<input type='hidden' value= " + var1 + " name='wallpostid'>... "

"...<input type='hidden' value='" + var1 + "' name='wallpostid'/>... "

Note the / and the two '

Also this line

    <b onclick= 'postcomment()' > </b>;

does not do anything as it is in javascript and not html section of the file.

First of all, your markup is a bit of a mess so no wonder there's a mess up amongst it. However I think your problem is that $userid is being passed through as a string and not it's value as a variable of some sorts.

Here's your markup tidied up a bit (but still leaves much to be desired) with some redundant stuff removed and the $userid echoed PHP-style.

<div id="147" onclick="showbox(this)"><a href="#">Comment</a></div>
<script>
function showbox(el) {
    el.innerHTML = el.id + '\
        <form name"="commentform" method="post" action="">\
            <input type="text" name="comment" value="Enter Your Comment" />\
            <input type="hidden" value="' + el.id + '" name="wallpostid" />\
            <input type="hidden" value="<?php echo $userid; ?>" name="userid" />\
            <input type="submit" name="send" value="Post your comment">\
        </form>';
}
</script>
document.getElementById(var1).innerHTML = var1 + " <form name = 'commentform'  method = 'post' action = ''><input type='text' name = 'comment' value='Enter Your Comment' /><input type='hidden' value= '" + var1 + "' name='wallpostid' /> <input type ='hidden' value = '$userid' name = 'userid' /> <input type='submit' name = 'send' value='Post your Comment' onClick='javascript:postcomment()' /></form><b onclick= 'postcomment()' ></b>";

A bunch of things are wrong:
1. ID's can't start with a numeral
2. Use onclick , not onClick
3. Don't put javascript: in your onclick
4. Your <b onclick ... is useless
5. Your postcomment function just submits the form - you don't need JS for that
6. Make sure you set an action for the form
Here's the complete fixed code:

<div id='div147'><u><b><font color='blue' onclick='showbox(147)'>Comment</font></b></u></div>
<script type='text/javascript'>      
function showbox(var1) {
    document.getElementById(var1).innerHTML = var1 + " <form name='commentform'  method='post' action='someurl.php'><input type='text' name='comment' value='Enter Your Comment'><input type='hidden' value='" + var1 + "' name='wallpostid'><input type='hidden' value='$userid' name='userid'> <input type='submit' name='send' value='Post your Comment'>  </form>';
}
</script>

While adding inner html some times run time errors are shown by some browsers. Reffer this question for better understandability for how to add inner html . Also I dont think you need onclick function for submitting form. It will submit automatically after clicking on submit

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