...

View Full Version : What's wrong with this?



ebay258
05-26-2005, 07:19 PM
Hi,

I'm strugling to get a script runwithin a script. As an example I made a simple script and put it in a function called SayHello. The function is supposed to run the script right?

Here is simple code:

<html>
<head>

<script type="text/javascript">
<!--
function SayHello( )
{
<script>
document.write("Hello!");
</script>
}
// -->
</script>

</head>
<body>

<script type="text/javascript">
<!--
SayHello( );
// -->
</script>

</body>
</html>


I can't see why it's not working, - anyone?

jshl_wiz
05-26-2005, 07:27 PM
remove the <script> tags in the code, it'll give u an error because u r mixing up HTML with JavaScript incorrectly... try this:


<html>
<head>

<script type="text/javascript">
<!--
function SayHello()
{
document.write("Hello!");
}
// -->
</script>

</head>
<body>

<script type="text/javascript">
<!--
SayHello();
// -->
</script>

</body>
</html>

ebay258
05-26-2005, 07:33 PM
Hi jshl_wiz, you must have missed what I said but that's not what I'm trying to do. I want the SayHello function to run a separate script - not a statement within it's own script.

jshl_wiz
05-26-2005, 07:42 PM
ok, let me see if i understand, you want to write a script WITHIN another script, so you would have to do something like:

<script>
<!--
function sayHello(){
document.write("<script>");
document.write("document.write('Hello!');");
document.write("</script>");
}
//-->
</script>
</head>
<body>
<script>
<!--
sayHello();
//-->
</script>

Are we in the same page now?? :confused:

ebay258
05-26-2005, 07:58 PM
Great thanks a lot. The first time some one gave me a straigh answer for this.

Now what if I wanted the function to write an external file like:


<script type="text/javascript" src="http://website.com/file.js'><\/script>

Sorry, I'm a newbie. I really appreciate the help.

ebay258
05-26-2005, 08:26 PM
bump!?

rlemon
05-26-2005, 08:37 PM
Great thanks a lot. The first time some one gave me a straigh answer for this.

Now what if I wanted the function to write an external file like:


<script type="text/javascript" src="http://website.com/file.js'><\/script>

Sorry, I'm a newbie. I really appreciate the help.


<script>
<!--
function sayHello(){
document.write("<script type='text/javascript' src='");
document.write("http://website.com/file.js");
document.write("'><\/script>");
}
//-->
</script>
</head>
<body>
<script>
<!--
sayHello();
//-->
</script>

jshl_wiz
05-26-2005, 08:43 PM
don't really know if that one's possible... if your purpose is to save space you could do the whole function with the bunch of "document.write"'s in a separate JS file and then call it from your page.

rlemon
05-26-2005, 08:46 PM
Please read this post.

It will awnser all of your questions.

http://codingforums.com/showthread.php?t=59766



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum