/    Sign up×
Community /Pin to ProfileBookmark

innerHTML and script tag

Hi,

Is there any way I can have a script tag “<script>” to be used in an innerHTML?

Thanks.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@savvykmsOct 30.2009 — perhaps
[code=php]
function changediv() {
document.getElementById("something").innerHTML = "<script langauge="javascript" type="text/javascript">nalert("test");</script>n";
}
[/code]


[code=html]
<div id="something"></div>
<a href="#" onclick="changediv(); return false;">Test</a>
[/code]
Copy linkTweet thisAlerts:
@rnd_meOct 30.2009 — Hi,

Is there any way I can have a script tag "<script>" to be used in an innerHTML?

Thanks.[/QUOTE]



just escape the closing tag:
[CODE]
"<script><[COLOR="Red"][B]/[/B][/COLOR]script>"[/CODE]


however, note that scripts added via .innerHTML don't normally fire.

you will have to either:

1. search the content string and extract script text, and eval each one.

2. use innerHTML, then get all the script tags in the injection's container, and eval the text of each one.
×

Success!

Help @michaelch511 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,
)...