/    Sign up×
Community /Pin to ProfileBookmark

event handler defined in innerHTML not work

Hi!I really get confused about the trouble, please give me a hand. I have the following code to create a button with its event handler in javascript:

<script language=”JavaScript”>
var obj=window.document.createElement(“form”);
obj.innerHTML=”<input type=’button’ value=’xx’ onclick=”alert(‘123’)”>”;
window.document.body.appendChild(obj);
</script>

the code worked on Mozilla and Opera and even my collegue’s IE!!
However, it just not work on my IE?
We have the same pathes, the same IE version, the same OS version and patches. Is there any problem about system settings I should check?

Thank you!!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@VladdySep 15.2004 — You started using DOM - keep doing it.
Copy linkTweet thisAlerts:
@lendleauthorSep 15.2004 — Thanks, Vladdy,

I think you suggest me to get rid of the innerHTML and use DOM only?

Yes, if I use

var button1=createElement("input");

button1.onclick=......

or

button1.attachEvent("onclick",......);

both will work well.

However, since we expect that the content of the innerHTML may be a string from our users, we have to make the innerHTML way work.

Thank you, and please kindly give me more suggestions.
Copy linkTweet thisAlerts:
@VladdySep 15.2004 — No conection beween innerHTML and user input...
Copy linkTweet thisAlerts:
@lendleauthorSep 15.2004 — Thanks, Vladdy,

We expect users to put an html code into an xml so we can read it using MSXML or just give us a function pointer which return the html code. Since our target user may not have javascript and dhtml experience, we cannot ask them just give us some dom node, and this is why we are using innerHTML.

Thanks, though.
Copy linkTweet thisAlerts:
@VladdySep 15.2004 — If you plan on taking some HTML code from someone and plug it straight into your page - you are asking for trouble.

Before you do that you need to verify the validity of such code. That is the reason CMS systems mostly operate with pseudo-codes (like BBCodes used by this and other forums).

Now, if that is some kind of CMS, why are doing it on the client side ? :rolleyes:
Copy linkTweet thisAlerts:
@lendleauthorSep 15.2004 — Thanks again, Vladdy,

Our problem is that, we are developing an UI-system which is beased on html and permits user to create its own plugin to insert into the UI. Since my boss insists that our target user only have html capability, we cannot ask our users to use activex control or else to create its UI plugin. Therefore, we have to suffer the trouble from innerHTML?
Copy linkTweet thisAlerts:
@janaka44Sep 23.2004 — I also tried the same coding u gave, it works when used inside a function, but when it is used the same way u've given , it gives an error saying the window.document.body object is not loaded. But i hope u're calling this code segment from an event, if this innerHTML part is coming from the user.
Copy linkTweet thisAlerts:
@David_HarrisonSep 29.2004 — Try this, I've written a pretty simple markup parser so that you can run through the code entered and use the DOM to append it to any element you like, the function "makeNewElement" retuns the object that it has just created.

I have had to use eval in this script because I couldn't think of a way around it, I know that it's frowned upon (although I don't know why), however it does that job and until I find something better it'll have to stay there.

Edit: One day I swear I'll learn RegExp's and put them to good use in a script just like this, honest! Come on Vladdy, convert this to RegExp's and put me to shame. :p

[upl-file uuid=fe664746-3587-4f50-8739-98bbf03354c3 size=2kB]parse.txt[/upl-file]
×

Success!

Help @lendle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...