Unknown Errors in Script - javascript

I'm trying to make a HTML/JS/CSS script that counts the number of days until some birthdays.
Last year, I made one, it still works, I copied and pasted the same script, and changed the names/dates and now it doesn't work.
Working one: http://jsbin.com/iFItOYo/16/edit
Broken one: http://jsbin.com/iFItOYo/14/edit

You made a couple of mistakes:
You forgot the closing bracket at this point:
fatima = new Date(thisYr,2,7)
if (fatima.getTime() < now.getTime()) {
fatima.setYear(nextYr)
} <-- this one is missing
In the beginning you state now = new Date, which should be now = new Date()
You forgot to close the <!-- that you begin in the top of your code

It's quite simple, you are missing the the closing tag of the comment '-->', and thus you get an unexpected end of input error.
Also putting your script inside of a comment is not such a good practice, use cross browser CSS if comments
Also, use indenting, and lower case HTML, with indenting as well.
In addition, your script tag is not valid in any HTML \ XHTML spec.
Hope this helps.
Another issue:
<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT"> which comes after the h3 opening tag is also missing.
Perhaps this question belongs to code review.

You deleted the <script ...> tag right after the opening <body> tag.

There is a lot of mistakes in your code. You should do something to view it clearly so that you can see your mistake easily like indent code, write all in lowercase (both javascript and html), comment and closing code more exactly...
To fix the one not run you must do these 3 points:
1 Like koenp answer, closing this :
falisha = new Date(thisYr,2,20)
if (falisha.getTime() < now.getTime()) {
falisha.setYear(nextYr)
} <== add this bracket
2 Missing the script tag before calling the document write function in body
<H3>The following are the numbers of days until class birthdays (2011-12):
<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT"> <== add this
3 Wrong variable in this line
if (anahi.getTime() < now.getTime()) {
anihi.setYear(nextYr) <== change it to anahi (of course)
}

Related

The Difference between /*<![CDATA[*/ and //<![CDATA[

I'm a Blogger blog user. I once see a template locating script inside CDATA tag like this:
<script type='text/javascript'>
/*<![CDATA[*/
------script------
/*]]>*/
</script>
While, I before see a similiar code like this:
<script type='text/javascript'>
//<![CDATA[
------script------
//]]>
</script>
The only different is that /*<![CDATA[*/ and //<![CDATA[. I wonder whether those both have different effect, or advantages. I also wonder the purpose of differing those. Here is always waiting insight from you, and your answer is appreciated.
There is no difference.
// comments out the line
/* comments out code until it finds a */ … which is at the end of the line in your example.
// comments out the text until the end of the line
/* comments out everything, irrespective of line endings, until it encounters */
Since the */ is at the end of the line in your example, there is effectively no difference. However, there is a reason why you sometimes see /* */ comments instead of // comments for a CDATA section. Specifically, if the HTTP response body was being assembled programmatically, and you didn't have control over it, and the process removed line break characters it deemed unnecessary (in order to minimize the size of the output), and your script immediately followed the CDATA comment (because the process overzealously removed a line break it shouldn't have), it might look like this:
//<![CDATA[var MyVariable = 'This statement does something.';
In this case, MyVariable would not be declared or assigned because the statement is really just part of the comment. But if /* */ were used, as in this example:
/*<![CDATA[*/var MyVariable = 'This statement does something.';
...the comment ends before the code begins and the code would be executed as intended.
It's a fine point, but that's the reason you see it done.

JavaScript RegExp replace HTML comments

I'm searching a way to replace all html comments from a string like browser does. (multilined and unclosed)
For example, I actually use /(<\!--[\s\S]*?-->)/gim but if the html comment is unclosed, it does not replace it.
Normally, if the comment tag is not closed, comment tag gets everything after open tag...
Is there a way to adapt the regexp (or any other regexp) to do the stuff ? (in JavaScript)
This will mark all comments also the one without end tag: <!-- some text -->
<!--[\s\S]*?(?:-->|$)
This will mark all comments also the one without end tag: <!-- some text //-->
<!--[\s\S]*?(?://-->|$)
This will mark everything from the first <!-- to the very end of the file
<!--[\s\S]*?(?:$) and regex set to `^$ don't match at line breaks`
This will mark everything from the first <!-- to the end of the line
<!--.*
I must agree that using regex like this is not good practice and you shouldn't do it... here's why.
Buuuut, as a matter of understanding regex better, you can make something optional like this:
/(<\!--[\s\S]*?(?:-->)?)/gim
I wrapped --> in parenthesis to group it together
I put a ? after that group to make it optional
(not necessary) I put ?: inside of the group to keep the regex engine from saving a back reference... it's a performance nuance.
Thanks to #Andie2302 for the help.
This regexp /<!--[\s\S]*?(?:-->|$)/gi work find.
Do not use the flag m!

HTML with commented JavaScript code

I am trying to parse a HTML code in order to extract all links in it. To avoid unavailable links I remove the commented code that begins with <!-- and ends with --> .Here comes the problem: In the HTML code I may find some JavaScript code, for example:
<html>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
if (document.images) {
var pic2 = new Image(); // for the inactive image
pic2.src = "pic2.jpg";
var title2 = new Image();
title2.src = "title2.jpg";
}
...
-->
and the weird thing is that the js code is commented but it still works. So, if I remove that code, the result won't be as expected. What should I do in order to identify when I'm facing with unused commented code and when that commented code is functional?
the weird thing is that the js code is commented but it still works
Those aren't comments. Is is just syntax allowed inside script (and style) elements that follows the comment syntax so that browsers which predate script and style don't render the code as text.
What should I do in order to identify when I'm facing with unused commented code and when that commented code is functional?
Write a real HTML parser, following the parsing specification, and then remove any comment nodes from the generated DOM.
As a dirty (but possibly quick) solution, you could just ignore comments inside elements marked as containing CDATA in the HTML 4.01 DTD.
the weird thing is that the js code is commented but it still works
There is nothing weird about it. The comments <!-- --> only work in HTML, not JavaScript. Your above code will still work since you've put these comments within the <script> tags.
The only difference it makes is that if the user has disabled JavaScript on his/her browser, he won't see the code printed on the browser (since HTML will parse those comments in the absence of JavaScript).
You need to comment out the whole <script> block. e.g.
<!-- <script>
...some javascript code...
</script> -->

Javascript if url equals function for adding text over only one page

What I want to do is pretty simple. Above my blog I want to have a text paragraph with an explanation of the site. However I do not want it to be at the top of every page on the site ONLY the index page. My host has a very restrictive format. I've already asked them for help and it isn't possible with their editor. I cannot edit all of the html but I can change some. Javascript seems to be allowed so I have been trying things like:
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
if (url==http://mysiteaddress/index.html)
{document.write('Welcome to my site')}
//-->
</SCRIPT>
Taking out the if statement, it successfully writes 'Welcome to My site'. So I'm wondering what is wrong with the if statement. I've also tried adding an 'else' in order to see output on any page but have not had any luck.
I obviously don't know much yet so any help would be appreciated. Thanks!
The url is available via location.href - not sure why you think url exists.
Besides that, mime types are lowercase and the language attribute is obsolete. So use <script type="text/javascript">.
Additionally it'd be better if you didn't use the infamous document.write but document.getElementById('someDiv').innerHTML = 'your html here';
Or get jQuery and simply write $('#someDiv').html('your html here');
Just to add, you can prepend an element to the body element like this:
var p = document.createElement("p");
p.innerHTML = "I am a paragraph";
document.body.insertBefore(p, document.body.firstChild);
You can try it here.
Reference: https://developer.mozilla.org/En/DOM/Node.insertBefore

Missing } in XML expression

I have an external javascript file that I want to, upon include, write some HTML to the end of the web page.
Upon doing so though I get the error Missing } in XML expression on the line that uses dropdownhtml.
Here is my code
var dropdownhtml = '<div id="dropdown"></div>';
$(document).ready(function(){
//$(document).append(dropdownhtml);
alert(dropdownhtml);
});
The XHTML webpage that includes this file does so like this:
<script type="text/javascript" src="/web/resources/js/dropdownmenu.js"></script>
Doing either append or alert throws up the same error, what is going wrong?
I got this error because I called an external JavaScript within an existing JavaScript, so ended up with:
<script type="text/javascript">
<script type="text/javascript">
code
</script>
code
</script>
Edit Your update changes the question a bit. :-)
There's nothing wrong with your quoted Javascript or with the script tag that includes it, the problem must lie elsewhere on the page.
The old answer:
If you're including Javascript inside an XML document, you must wrap it up in a CDATA section, or you'll run into trouble like this because the XML parser neither knows nor cares about your Javascript quotes, and instead seems markup (your <div>s in the string).
E.g.:
<foo>
<bar><![CDATA[
var dropdownhtml = '<div id="dropdown"></div>';
$(document).ready(function(){
//$(document).append(dropdownhtml);
alert(dropdownhtml);
});
]]></bar>
</foo>
Naturally you need to ensure that the ]]> sequence never appears in a string (or comment, etc.) your script, but that's quite easy to do (for instance: "Be sure to interrupt the end sequence with a harmless backslash like this: ]]\>; that escape just resolves to > anyway.")
There's definitely a missing ); at the end of your code sample. Don't get where there may be a missing } though.
I have empty script on my page
<script src=""></script>
And this leads to such error

Categories

Resources