/    Sign up×
Community /Pin to ProfileBookmark

Script appears to be failing while instantiating a new object

I have a very simple html page with java script embedded to create an object and display some properties of the object. But it appears to be failing when I instantiate the new object. I confirmed that by removing the line where I am instantiating it and re-running the page with some alert messages. If I remove the line starting with var log…., the alert(“test”) display fine, else it does not display

<!DOCTYPE html>
<head>
<title>YouCube – The Blog for Cube Puzzlers</title>
<script = “text/javascript”>
function Blog(body, date) {
this.body = text;
this.date = date;
}

var blog = [ new Blog(“Got the new cube I ordered”, “08/10/2012”) ];
alert(“test”);
numEntries = blog.length;
alert(numEntries);
function showBlog(numEntries)
{
numEntries = blog.length;
alert(numEntries);
if (!numEntries)
{
numEntries = blog.length;
alert(numEntries);
}
}
</script>

</head>
<body>
<h2>YouCube – The Blog for Cube Puzzlers</h2>
<img src = “cube.png” alt = “YouCube” />
<div id=”blog”></div>
<input type = “button” id = “showall” value = “Show all Blog Entries” onclick = “showBlog()” />
</body>
<?html>

Any ideas/thoughts on what I could be doing wrong?

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@blasphemyFeb 14.2014 — Hi. In function 'Blog(...)' you have 'this.body = text'. 'text' is not defined! I think it should be 'this.body = body'
Copy linkTweet thisAlerts:
@threezerousauthorFeb 14.2014 — Hi. In function 'Blog(...)' you have 'this.body = text'. 'text' is not defined! I think it should be 'this.body = body'[/QUOTE]

Oh man...that was a bummer. I have been so focussing on the var blog instantiation, didn't go back and check carefully.

Thanks much !!!?
Copy linkTweet thisAlerts:
@blasphemyFeb 14.2014 — Do you use 'JavaScript console'? In all modern browsers there are 'Tools for webmasters'. One of the most important part of it is 'Javascript console'. I copied your code and pasted into blank file on my computer. I opened it in my browser (with JS console opened) and I found the error very quickly.
Copy linkTweet thisAlerts:
@rootFeb 15.2014 — Can you please WRAP your code in forum tags, makes it easier to read for one. Your going to get help quicker as well.

So without wading through that wall of text, I am going to have a stab in the dark here and say that you are either trying to access a property not yet available in the document body or an item that has not fully initalized, in both cases, use of window.onload would be a cure.
×

Success!

Help @threezerous spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...