Joshua Joshua - 5 months ago 9
HTML Question

Compiling JavaScript and HTML code in Visual Studio IDE through ASP.NET

I have been through 3 simple tutorials on how to get started with JavaScript using VS 2010 and they all unanimously describe the basic steps of creating a project through

File
,
New
,
Project
,
ASP.NET Empty Web Application
and then adding a JavaScript file through
Add
,
New Item
and finally adding an HTML page to the project.

The contents of my two files are as follows, as described in all basic tutorials, like a HelloWorld program:

JScrip1.js

function add(a, b)
{
return a + b;
}


HTMLPage1.htm

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>My page title!</title>
<script type="text/javascript" src="JScript1.js" />
</head>

<body>
<script type="text/javascript" >
alert("Sum of 3 and 4 = " + add(3, 4));
</script>
</body>

</html>


In all the tutorials, upon doing a right-click on the HTML component and choosing
View in Browser
, it compiles and shows an alert window with the result (
7
) but mine only shows the tab with the title, and a pop-up message saying that
ASP.NET Development Server is running on port 56044


The page that opens has the following URL:
http://localhost:56044/HTMLPage1.htm


What am I missing? Why is my html page completely blank?

RESOLVED by changing my HTML script to the following, thanks to Jared

<!DOCTYPE html PUBLIC>
<html>

<head>
<title>My page title!</title>
<script type="text/javascript" src="JScript1.js"></script>
</head>

<body>
<script type="text/javascript" >
alert("Sum of 3 and 4 = " + add(3, 4));
</script>
</body>

</html>

Answer

Is your browser blocking popups? If so, you can change the alert() command to console.log() command. That should be safer. (Press F12 to open the browsers developer tools and look for a console tab to see the console.log() output)

FYI: The console log will also show javascript errors and such. Like if it can't load the JScrip1.js file.