0

I'm trying to get the following Javascript work (it should be an embedded survey from formstack.com that shows up on specific days). However, when I try to view it all I get displayed is the string (without the quotes) "'); }" What is wrong?

<head>
<script type="text/javascript">

var theDate = new Date();
var dayOfWeek = theDate.getUTCDay();

// Returns true if the restaurant is open
function isOpen()
{
    //I'll fill this in later, for now, return true
    return true;
}
</script>

</head><body>
<script type = "text/javascript">
if(isOpen())
{
document.write('<script type="text/javascript" src="http://www.formstack.com/forms/js.php?1134414-uqmj2UXxEw-v2"></script>');
}
</script>
</body>
4
  • 3
    Not the most descriptive question title... :/ Commented Nov 6, 2011 at 0:03
  • Where is your doctype and html tag? Commented Nov 6, 2011 at 0:06
  • This works: jsfiddle.net/wYrze/1 Commented Nov 6, 2011 at 0:06
  • 1
    this does work, describe your problem... (What is the error, in what browser etc...) most of the time when you inject JS to the HTML you need to give it some time so the browser can interpret it.. Commented Nov 6, 2011 at 0:10

4 Answers 4

2

Don't have a </script> tag in your string, that ends the script tag.

document.write('<scr'+'ipt type="text/javascript" src="http://www.formstack.com/forms/js.php?1134414-uqmj2UXxEw-v2"></scr'+'ipt>');
Sign up to request clarification or add additional context in comments.

5 Comments

So why do you break up the <script part? I've known this method worked for some time, but haven't ever known why it works.
@JaredFarrish: Just to be on the safe side. It works simply because </scr'+'ipt> is not interpreted as an ending tag for the script.
Just wondering... Why does JS interpret a script tag in a string as the ending tag for the calling statement? (Which is what I think you're saying, right?)
@Guffa I believe it's the constucte </ that has to be guarded against, per spec, but this is likely guarded to only need to protect against </script> in modern browsers. <script> -- or generally, any < not in the form </ -- is perfectly fine/safe within a script block (for HTML).
@JaredFarrish: Because the HTML tags are parsed before the script.
2

You can't have a string containing '</script>' inside a SCRIPT element. Consider loading additional scripts like so:

var script = document.createElement( 'script' );
script.src = 'http://www.formstack.com/forms/js.php?1134414-uqmj2UXxEw-v2';
document.body.appendChild( script );

Your preferred JavaScript library probably contains a dedicated function for this. For instance, I use jQuery, where it's done like so:

$.getScript( 'http://www.formstack.com/forms/js.php?1134414-uqmj2UXxEw-v2' );

Comments

0

I've heard stories of javascript failing to execute inside document.write... try breaking it up:

document.write("script type = " + "text/javascript" + "src='http://www.formstack.com/forms/js.php?1134414-uqmj2UXxEw-v2'>" + "/script"); 

(greater and less than signs removed, you should know where to put them)

1 Comment

"You should know where to put them" - Huh?
-1

I wonder if the / of is being see as the escape character. Something that you can try is

String s = @"...." where .... is the text.

The @ tells the compiler to treat everything in "'s as a literal. I know that this works in both C# and Java. I am not 100% sure about javascript.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.