What's wrong with my Javascript code?

2019-09-17 03:33发布

问题:

I'm trying to create some javascript code that will display a javascript form (from formstack.com) on my website certain days of the week. However I can't seem to get it working. I think the first portion is correct, but when it gets to having javascript code to display other javascript I'm confused. Please help!!! Thanks

<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())
{
<script type="text/javascript" src="http://www.formstack.com/forms/js.php?1134414-uqmj2UXxEw-v2"></script><noscript>
<a href="http://www.formstack.com/forms/CampusEnterprises-chopped_greens_order_form__copy" title="Online Form">
Online Form - Chopped Greens Order Form- COPY</a>
}
</script>
</body>

回答1:

You're trying to display raw HTML inside JavaScript which won't work.

<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>

That said, even better would be to use a server-side language instead of JavaScript for things like this.