This question already has an answer here:
Does HTML comment <!-- act as a single-line comment in JavaScript, and why?
(1 answer)
Closed 7 years ago.
I have taken on a project that someone else has worked on and I am running into a bunch of script blocks formatted like this:
<script>
<!--
$('...').live('change', function() {
if (...) {
$('...').hide();
$('...').show();
} else {
$('...').show();
$('...').hide();
}
});
//-->
</script>
They are not even the correct comment format for JavaScript.
The code still works but should I worry that this might break in the long term if I start adding to it?
Is this just blatantly wrong and should I remove all the invalid comments?
To formalize my comment.
Back in the day not all browsers supported JavaScript. To make the JS disappear it would be wrapped in an HTML comment and the browser would effectively ignore it.
This hasn't been relevant for some time now and it can be safely deleted.
It can also be left in without harm, but ew.
By way of example, this question from 2009 asks the question re: relevancy, not what it actually is.
This also means I'm old :(
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
Having a bad day. This one has stumped me all morning. All the solutions I've found have stopped one step short of where I need to go.
I have a legacy PHP/JS app that I'm working on. Rather than trying to explain it, I'll just show what I need to do.
<?php
$phpDate_1 = new Date($someDate);
$phpDate_2 = new Date($someOtherDate);
//...There are a bunch of these
$phpDate_n = new Date($endOfTime);
<script language="javascript">
function myFunction() {
var line = aUserSelection; //an int from user which tells me what date to use
//Next line is the problem. I'm trying to pull the month from the appropriate PHP date into the JS variable.
var theMonth = "<?php echo $phpDate_" + line + "->getMonth();?>";
}
</script>
?>
I must have tried 20-30 combinations of single and double quotes, escapes, dots, pluses, and so on, but I keep getting errors over the "line" part. Unexpected character, encapsed strings, etc.
Hoping someone can point me in the right direction because my brain is fried at this point. Answers in pure JS and PHP only please because that's how the app is built. Thanks.
You need to close your php (?>) before outputting the javascript to fix the syntax error that you got.
However, with that said, you are trying to incorporate the javascript line variable into the variable name for $phpDate, to generate something like $phpDate_1.
If you don't want to go with an AJAX solution, your best bet would be to output each line's date into a javascript array. This is strongly discouraged, but if this is a legacy application that you cannot make many changes to, this might be your only option.
This question already has an answer here:
innerHtml command not working Advise please
(1 answer)
Closed 6 years ago.
I am trying to learn the javascript debugging and I am followint the article here[linke here][1]
[1]: http://meeech.amihod.com/getting-started-with-javascript-debugging-in-chrome/ I was able to follow along the tutorial.My code is
<script>
var value="hang";
function test(){
setInterval(function(){
switch(value){
case'hang':
document.body.innerHtml="Hang in there";
break;
case'up':
document.body.innerHTML="Up";
break;
default:
document.body.innerHTML="No good!";
}
},1000);
};
</script>
Ater inspecting in debugger,it shows that it reached to document.body.innerHtml="Hang in there";But my html page isnot showing the output Hang in there
Though it may seem simple question, I am not getting into it. Please help.
Also I cannot find Add to watch option on right click as stated in tutorial.
JS property names like innerHTML are case-sensitive. Try changing it to ….innerHTML="Hang in there".
This question already has answers here:
Changing input value while still allowing the user to type
(2 answers)
Closed 9 years ago.
If the user types:
my name is jackson5 model number 5g1 |nR%1b and I would 644 like to say 55 hello to all the tags of html in the world.
I'd like to replace the 55 hello with 55hello while they continue to type with no hiccups or interuptions to the input box.
here is my code so far. I have the regex statement in there but not sure how to replace the space between 55 hello so that it becomes 55hello.
<script type="text/javascript">
function isValid(strQuery){
var regStatement =/[0-9]+ hello/;
if (regStatement.test(strQuery.value)) {
strQuery.value = strQuery.value.replace("$2","");
}
}
</script>
<input type="text" id="wtf" name="search_query" onkeyup="isValid(this);" />
try this
function isValid(strQuery){
strQuery.value = strQuery.value.replace(/((\d+)\s+)(?=hello)/,"$2");
}
you don't need to test it first, if the string doesn't match, it won't replace anything.
I don't have enough reputation to post comments, so I will have to resort to an 'answer' -- be careful with this approach. I've tested my solution and also that posted by #leonhart above (his is much better, so I will not bother posting mine). What both have in common is that both cause TWO calls to isValid, in case if your input box has a history of similar inputs, and starts to provide suggestions (didn't occur in Chrome, but did in Firefox). Not sure what's causing it, but, apparently, either the event handler is registered twice, or something along those lines.. good luck figuring that out! Or maybe someone else here can explain the cause... and the workaround =)
This question already has answers here:
What does the leading semicolon in JavaScript libraries do?
(6 answers)
Closed 9 years ago.
It's looks extraneous, but it must do something.
ref: https://github.com/quirkey/sammy/blob/master/examples/hello_world/index.html
<script type="text/javascript" charset="utf-8">
;(function($) {
//snip
});
$(function() {
//snip
});
})(jQuery);
</script>
This is to make sure previously loaded code that could have not been terminated with a semicolon gets terminated properly, otherwise it would result in an error. You could say it makes the code more tolerant to other people's bugs.
Update: I tested this out and at least in current Chrome and Firefox it makes no difference whether a previous statement is still open, so the semicolon has no effect on that. Idea: that might still be a problem with very old browsers, but I it is just an idea I didn't verify.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
$(‘<element>’) vs $(‘<element />’) in jQuery
I am used to write $('<div>').
But today I saw a presentation about advanced-jquery by john-resig who use the following syntax $('<div/>').
http://loft.bocoup.com/john-resig-advanced-jquery/
To me they seem to produce the same output.
My question is: is there some difference between
$('<div>') and $('<div/>')?
No, jQuery will normalize those statements into the exact same.
In some earlier version of jQuery tho, it happend to be that <div> was actually faster than <div/> for whatever reason. I don't know yet, if that still applies.
http://jsperf.com/jquery-constructor-performance
Seems like this bug/feature is no longer true.
<div>, <div/>, <div></div>, and even <div/></div> (Yes, this one will only create one element) all trigger the singleTag regular rexpression which makes jQuery to call document.createElement("div"). It was never passed to any html parser at all.
Here's the regex that you can play with, if it returns true, it will be document.createElement'd
var rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/;
<div> is an opening tag. <div/> is a self-closing tag. In this context, however, there is no difference.