/    Sign up×
Community /Pin to ProfileBookmark

document.write function call

Hi! I need help with calling a function from document.write. I want to call the function good() by pressing a button in a document created by javascript, but it doesn’t work.

[code]

<html>
<head>
<title>TITLE</title>

<script language =”Javascript”>

function good(){
alert(“good”)
}

function myFunction(){

document.write (“<html><head><title>NEWDOCUMENT</title> </head>”);
document.write (“<body><p><button onclick=’good();’> Good? </button>”);
document.write (“</p></body></html>”);
document.close ();
}

</script>
</head>

<body><p>
<button onclick=”myFunction()”> Call myFunction </button><br />
</p></body>
</html>
[/code]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledMar 25.2009 — it wouldn't work, despite function [B]good[/B] is defined, when write to the document everything is discharged from the browser. so, when you write, it is actually a new document that doesn't define such function. you would have to write a script tag with such function as well. however, be careful when writing script tag, it may give you [U]unterminated literal string[/U] error. to prevent it, split the closing that in two string:
<i>
</i>document.write("&lt;script&gt; rest of your content&lt;/scri"+"pt&gt;");
Copy linkTweet thisAlerts:
@felgallMar 25.2009 — or

document.write("&lt;script&gt; rest of your content&lt;/script&gt;");
Copy linkTweet thisAlerts:
@ZeroKilledMar 25.2009 — that's cool, felgall! is more compact. since i had abandoned code that write to document a very long time ago, i don't know how many options there are to prevent such error. ?
Copy linkTweet thisAlerts:
@DrakoonauthorMar 25.2009 — Thanks for the answers!!!
Copy linkTweet thisAlerts:
@luis11zivalevoMar 25.2009 — Hi i need help for this problem how i can implement this :when i click on the button of this script:i can see that the direction of shadow is changed but i need when i press the button to change also the color of the shadow not only the direction how i can make that

please someone help me?

THANKS!!!

<title>zadaca2</title>

<script language="JavaScript">

var theDirection=45;

var theColours=["green"+"red"];

var thisColour=0;

function rotateShadow() {

theDirection=45;

document.getElementById('text').filters("shadow").direction=theDirection;

if (theDirection==360) {

theDirection=0;

}

}

</script>

</head>

<body>

<input type="button" value="Change Shadow" onclick="rotateShadow();">

<h1 id="text" style="position:absolute; top:60; left:70;font-family:Lucida Console; font-size:35pt;color:black; padding:11px;filter:shadow(direction=-45, color=green,red)">

Shadow Filter Dynamically

</h1>

</body>

</html>
Copy linkTweet thisAlerts:
@luis11zivalevoMar 25.2009 — Hi i need help for this problem how i can implement this :when i click on the button of this script:i can see that the direction of shadow is changed but i need when i press the button to change also the color of the shadow not only the direction how i can make that

please someone help me?

THANKS!!!

<title>zadaca2</title>

<script language="JavaScript">

var theDirection=45;

var theColours=["green"];

var thisColour=0;

function rotateShadow() {

theDirection=45;

document.getElementById('text').filters("shadow").direction=theDirection;

if (theDirection==360) {

theDirection=0;

}

}

</script>

</head>

<body>

<input type="button" value="Change Shadow" onclick="rotateShadow();">

<h1 id="text" style="position:absolute; top:60; left:70;font-family:Lucida Console; font-size:35pt;color:black; padding:11px;filter:shadow(direction=-45, color=green)">

Shadow Filter Dynamically

</h1>

</body>

</html>
Copy linkTweet thisAlerts:
@ZeroKilledMar 25.2009 — you should had open a new thread if the topic is different. however, place this line inside your rotateShadow function. of course, you would have to give a different color to variable [B]theColour[/B] because green is what you initially have:
<i>
</i>document.getElementById('text').filters("shadow").color=theColours[thisColour];
×

Success!

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