Javascript with HTML and document Object

Batch, ASP, JScript, Kixtart, etc.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
bissettbd
Posts: 1
Joined: Fri Apr 10, 2009 5:03 am

Javascript with HTML and document Object

Post by bissettbd » Fri Apr 10, 2009 5:03 am


I have tried to run the following script (which I pasted in from a Dreamweaver website) but Primal Script 2009 throws errors for all HTML code and the Java document.write object.

How can I prevent this from happening, and run and debug the script as it would in a browser?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- ... dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title></head>
<BODY>
<CENTER><SCRIPT LANGUAGE="JavaScript">

<!-- Beginvar Hours, Mins, Sec;var Year, Month, Day;var Stamp, DTStamp;
Stamp = new Date();alert(Stamp);Year = Stamp.getYear() + '';if (Year < 2000) Year = 1900 + Year;Month = Stamp.getMonth() + '';if (Month.length == 1) {Month = '0' + Month;}Day = Stamp.getDate() + '';if (Day.length == 1) { Day = '0' + Day; }Hours = Stamp.getHours() + '';if (Hours.length == 1) { Hours = '0' + Hours; }Mins = Stamp.getMinutes() + '';if (Mins.length == 1) { Mins = '0' + Mins; }Sec = Stamp.getSeconds() + '';if (Sec.length == 1) { Sec = '0' + Sec; }
DTStamp = Year + Month + Day + Hours + Mins + Sec;//DTStamp = new Date("yyyy,mm,dd,hh,mm,ss")document.write('<font size="2" face="Arial"><B>' + DTStamp + '</B></font>');//document.write('<font size="2" face="Arial"><B>' + Hours + ":" + Mins + Time + '</B></font>');// End -->alert(createtktnbr());

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Javascript with HTML and document Object

Post by jvierra » Fri Apr 10, 2009 6:02 am

I added closures to script.

Al code that shows copiles and debugs correctly but this line is bad:

alert(createtktnbr());
The called function does NOT exist.

It is generally not very easy to cut and paste website HTML and have it work. Most web HTML uses external files, classes, libraries and makes back-end calls to a server. What you see is the generated HTML from teh web service. It is designed to work together with the web server and not generally work independently.

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Javascript with HTML and document Object

Post by jvierra » Fri Apr 10, 2009 7:31 am

It's an HTML page with JavaScript. It can only be used as an HTML page.

Her is my fixed version. It runs but does nothing as most of the page and code is mssing.

Code: Select all

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<BODY>
<CENTER>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var Hours, Mins, Sec;
var Year, Month, Day;
var Stamp, DTStamp;
Stamp = new Date();
alert(Stamp);
Year = Stamp.getYear() + '';
if (Year < 2000) Year = 1900 + Year;
Month = Stamp.getMonth() + '';
if (Month.length == 1) {Month = '0' + Month;}
Day = Stamp.getDate() + '';
if (Day.length == 1) { Day = '0' + Day; }
Hours = Stamp.getHours() + '';
if (Hours.length == 1) { Hours = '0' + Hours; }
Mins = Stamp.getMinutes() + '';
if (Mins.length == 1) { Mins = '0' + Mins; }
Sec = Stamp.getSeconds() + '';
if (Sec.length == 1) { Sec = '0' + Sec; }
DTStamp = Year + Month + Day + Hours + Mins + Sec;
//DTStamp = new Date("yyyy,mm,dd,hh,mm,ss")
document.write('<br /><font size="2" face="Arial"><B>' + DTStamp + '</B></font>');
//document.write('<font size="2" face="Arial"><B>' + Hours + ":" + Mins + Time + '</B></font>');
// End -->
alert(createtktnbr());
function createtktnbr() {
//Function to Create Ticket Number B Bissett
var Hours, Mins, Sec;
var Year, Month, Day;
var Stamp, DTStamp;
Stamp = new Date();
Year = Stamp.getYear() + '';
if (Year < 2000) Year = 1900 + Year;
Month = Stamp.getMonth() + '';
if (Month.length == 1) {Month = '0' + Month;}
Day = Stamp.getDate() + '';
if (Day.length == 1) { Day = '0' + Day; }
Hours = Stamp.getHours() + '';
if (Hours.length == 1) { Hours = '0' + Hours; }
Mins = Stamp.getMinutes() + '';
if (Mins.length == 1) { Mins = '0' + Mins; }
Sec = Stamp.getSeconds() + '';
if (Sec.length == 1) { Sec = '0' + Sec; }
DTStamp = Year + Month + Day + Hours + Mins + Sec;
return (DTStamp);
}
</SCRIPT>
</CENTER>
<center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided

by <a href="The'>http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
</body>
</html>
jvierra2009-04-10 14:41:17

User avatar
jvierra
Posts: 13736
Joined: Tue May 22, 2007 9:57 am
Contact:

Javascript with HTML and document Object

Post by jvierra » Fri Apr 10, 2009 8:10 am

I just checked with PrimalScript 2009 and, as I suspected, you still can't use an internal debugger to debug HTML. You need to install teh WIndows Debugger and use that fot HTML code.

You can. however, extract javascript into a JS file and debug that but it can have NO references to the DOM as there is no HTML document.

To debug against client side code yo should look into Visual Web Developer Express. It will debug client side code easier than teh MSD engine does alone.

PrimalScritp is good for basic page edits and changes but requires a pretty good knowledge of HTML/JavaScript to use as a page developer although I use it all of the time for HTAs. I always separate my code from teh HTML so I can separately debu the sripts before adding them to teh page. This takes considerable knowledge of programming and HTML to accomplish so I don't recommend it for beginners.





Locked