Hi I am having a beast of a time removing comments from html code (so innerHTML counting works in IE). I admit I am not the brightest bulb :) I do not have access to (knowledge of) jquery or the source as such to remove them. I am terrible with regex so I havent tried this
I tried the following code
<script>
function clean(node)
{
for(var n = 0; n < node.childNodes.length; n++)
{
var child = node.childNodes[n];
if
(
child.nodeType === 8 ||
(child.nodeType === 3 && !/S/.test(child.nodeValue))
)
{
node.removeChild(child);
n--;
}
else if(child.nodeType === 1)
{
clean(child);
}
}
}
</script>
But this is giving me error in all browsers:
SyntaxError: missing ) after for-loop control
for(var n = 0; n < node.childNodes.length; n++)
Any ideas would be great. There are a bunch of jquery scripts on the page but I have no control over these and they do not have any errors that occur so I do not think this is related.
Thank you in advance
After you remove the comments via Javascript, they will still be visible in View Source since it shows the source from before the Javascript ran.
If you don't want comments to be visible to the user, don't use HTML comments. Use comments in a serverside language inside the server-side blocks. Like:
<%
//comment: printing variable x
out.print(x);
%>
I would start by replacing "< ;" with "<", and the same goes for your ampersands.
Related
So I am attempting to code a chess board with the limited knowledge that I have and I have been surprisingly successful. But there has been something weird.
In a for loop that I have to check all the possible moves and narrow it down (I know not the most efficient, but I'm trying to do this with as little help as possible). What I'm doing is basically going through a couple arrays to try to do it.
And when I do
for (i = 0; i < legalMoves.length; i++) {
if (legalMoves[i] > 63 || legalMoves[i] < 0) {
legalMoves.splice(i, 1)
i--
}
console.log(squareInfo[legalMoves[i]][2]) // line to pay attention to
}
It works perfectly as expected, but when I do
for (i = 0; i < legalMoves.length; i++) {
lM = squareInfo[legalMoves[i]] // variable to pay attention to
if (legalMoves[i] > 63 || legalMoves[i] < 0) {
legalMoves.splice(i, 1)
i--
}
console.log(lM[2]) // line to pay attention to
}
The error is TypeError: Cannot read property '2' of undefined
Which is really weird.
Somethings to note is that my arrays contain no undefined characters, and when I run it without the variable it works perfectly and as expected.
Does anyone know why this would happen, as being able to use the lM variable would make things a lot easier for this process.
To clarify, the question is why is the same thing when in a variable coming out as an error.
The real problem is that your lM is initialized with the i that has the wrong value, since that legalMoves[i] is not accepted.
You should do the lM = ... part only when the i passes the check.
for (i = 0; i < legalMoves.length; i++) {
if (legalMoves[i] > 63 || legalMoves[i] < 0) {
legalMoves.splice(i, 1)
i--
} else {
lM = squareInfo[legalMoves[i]] // variable to pay attention to
console.log(lM[2]) // line to pay attention to
}
}
But a better approach would be to remove the invalid values from the legalMoves array before the loop so you do not have to do this tricks in a loop.
const onlyLegalMoves = legalMoves.filter((move) => (move >= 0 && move <= 63));
for (i = 0; i < onlyLegalMoves.length; i++) {
const legalMove = onlyLegalMoves[i];
console.log(squareInfo[legalMove][2]) // line to pay attention to
}
Finally if you only want to find the first really valid move, you could just use .find instead of the whole loop.
const moveToUse = legalMoves.find( (move) => (move >= 0 && move <= 63) );
console.log( squareInfo[moveToUse][2] );
Tradis, The way you are using array to store in separate variable is the real problem. In Javascript, when you use simple variable javascript actually create new memory space and assign it to that memory space.
But when you are assigning array or object to new javascript variable, it just make a reference to that specific variable, not make a copy of that variable.
So, when you assign your array to variable IM and then you delete the i from your actual array. So, IM became undefined, because that specific index is being removed.
The actual issue is in your if condition, you can check it by commenting the if condition.
If you really want to use this variable technique, please use deep copy to make an actual copy of your legalMoves array.
I am stuck in somewhere in my code and need your help. Could be weird but important for me to achieve something in code.
I am writing a code snippet in my test cases under angular project. I need to pick an bs-tooltip-container element from HTML but they could be multiple.
So i need to add if loop conditions like
if(index === 0) {
fixture.debugElement.nativeElement.nextElementSibling.children[1].innerText.trim()
}else if(index === 1) {
fixture.debugElement.nativeElement.nextElementSibling.nextElementSibling.children[1].innerText.trim()
}else if(index === 2) {
fixture.debugElement.nativeElement.nextElementSibling.nextElementSibling.nextElementSibling.children[1].innerText.trim()
}
If you noticed in above code then I am multiplying .nextElementSibling based on index count
They appended on the fly so we could not target them directly.
I am trying to use FOR LOOP so if I have multiple then I can repeat .nextElementSibling
I wrote some code like below
public static getToolTip(index = 0): DebugElement[] {
for (let i = 0; i < index+1; i++) {
tooltipNodes.push(fixture.debugElement.nativeElement`${nextElementSibling}.repeat(index)`.children[1].innerText.trim())
}
But this is not working for me. It makes mix of JS syntax with string. I do not know what should be write approach.
Basically I wanted to know How can I repeat .nextElementSibling multiple times in code snippet?
What you need could be something like
fixture.debugElement.nativeElement.parentNode.querySelector(`bs-tooltip-container:nth-of-type(${index})`);
or
fixture.debugElement.nativeElement.parentNode.getElementsByTagName('bs-tooltip-container')[index];
my javascript knowledge is pretty poor. I'm trying to run a script with greasemonkey on http://www.twitch.tv/directory/all to remove certain kinds of streams from the list based on an image provided next to a shot of the stream(like picture of hearthstone, minecraft etc.). Here's the code:
//what you want to remove
var killIt=["http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-138x190.jpg", "http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-138x190.jpg", "http://static-cdn.jtvnw.net/ttv-boxart/Minecraft-138x190.jpg"];
var el = document.getElementsByClassName("boxart");
//runthrough elements killing certain ones
for (i = 0; i < el.length; i++) {
for (j = 0; j < killIt.length; i++) {
if (el[i].src == killIt[j]) {
var ely = el[i].parentNode;
ely.parentNode.removeChild(ely);
}
}
}
So i tried it on w3schools site and the code works fine, but when i try to actually run it in twitch.tv it does nothing(seemingly).
Am i missing something about parent nodes? Or greasemonkey?
The second for should be j++. Also you can use .indexOf to test if the URL is listed in the array to avoid a other for loop.
So, I've narrowed down my error (well, at least the first one) to this function:
var genArray = function () {
var arr, len, i;
if(arguments.length > 0) {
len = [].slice.call(arguments, 0, 1)[0];
arr = new Array(len);
for(i = 0; i < len; i++) {
arr[i] = genArray.apply(null, [].slice.call(arguments, 1));
}
} else {
return null; //or whatever you want to initialize values to.
}
return arr;
}
Then, I get a very unhelpful error:
error on line 71 at column 23: StartTag: invalid element name
Below is a rendering of the page up to the first error
Now, the function is decidedly not on line 71 (perhaps it is in the compiled ePub, but I have no idea how they correlate). Further, I have no idea what that error means in a JavaScript context. Also, this code works fine in a browser (Safari included).
Any ideas what could be causing the issue?
EDIT: On a whim, I checked whether [] was the problem by changing it to Array(). No luck.
Okay, so I discovered a solution to my problem. I just needed to surround my JavaScript in CDATA tags like so:
//<![CDATA[
var genArray = function () {
var arr, len, i;
if(arguments.length > 0) {
len = [].slice.call(arguments, 0, 1)[0];
arr = new Array(len);
for(i = 0; i < len; i++) {
arr[i] = genArray.apply(null, [].slice.call(arguments, 1));
}
} else {
return null; //or whatever you want to initialize values to.
}
return arr;
}
//]]>
I discovered this by using the epubcheck tool which said something to the effect that the file must have properly formed characters or something. I don't recall the exact message. Anyways, this reminded me of a problem I had in a script where I used some unicode characters. I remembered about CDATA which solved it. Then I found this stackoverflow question which basically says it's necessary for when your pages must be interpreted as XML/XHTML as well, which is the case for ePubs.
So, moral of the story is wrap javascript in CDATA tags for ePubs or iBooks.
EDIT: It should be noted that it's worth doing this around all of your JavaScript. The issue in my case was the < less than operator being interpreted as the start of a tag. However, it is probably cleaner to just include the CDATA tag around all of your JavaScript rather than trying to isolate sources of the issue.
EDIT 2: In the interest of aggregating information to whoever finds this answer useful, it should also be noted that having all of one's JavaScript in external files probably also works (according to the source linked in the answer to the question I've linked to). I don't care to test this at the moment, but it should work because the external JavaScript will not be parsed as XML like it is inside of a <script> tag.
The error you report indicates the XHTML file source is in error. I would take a look at, uhh, line 71 column 23 of the XHTML file in question. What's there? Could it possibly be <StartTag>? Is the XHTML being generated programatically somehow? EPUBs are not "compiled"; they are just zipped, and this line/column information refers to the actual position in the XHTML file in the EPUB. What does epubcheck say?
This kind of error message would not be generated by problems in any dynamic HTML created via script; those would result in a DOMError.
My guess is that iBooks is finding some error in the function at parse time, which terminates the parsing process before the XHTML parsing is completed and the XHTML error can be reported. However, I can't imagine what the error might be; I doubt it's the missing semi-colon at the end of the function, but could possibly be depending on what's on the next line.
Totally minor point, but
len = [].slice.call(arguments, 0, 1)[0];
is the same as
len = arguments[0];
Sounds to me more like an XHTML error. When you run in the browser if you are not opening it as an XHTML file, do so and see if it breaks. Browsers tend to be more lenient than EPUB readers. You are most likely creating some sort of invalid HTML element with your slices, it would be great to have the full page to identify exactly what 'getArray()' is returning...
My background is in java and right now Im getting into learning Javascript. (I'm porting this java code that I made into javascript but it behaves a bit different than I expected?? If I use "console.log()" instead of "document.write" I'm NOT getting the same result..why?
thanks 4 yr time! Some insight will be very appreciated!
var counter = 1;
function println(str) {
console.log(str);//
}
for (var i = 1; i <= 5; i++) {
for (var j = i; j < 5; j++) {
document.write("#");
// println("#");
}
for (var k = 1; k <= counter; k++) {
document.write("*");
// println("*");
}
document.write("<br/>");
//println(" "); <--- "\n" ?
counter+= 2; //
} // ends application
document.write() is for printing content to the page of your document, while console.log() is used predominantly for diagnostic/debug information to be emitted in the console of your web browser. Specifically, document.write() is intended to be consumed by the viewer of your page, while console.log() is generally not.
Console.log logs stuff into browser console. Install Firebug (getfirebug.com) and you will see your logs.
Also there is nice description about how it works http://getfirebug.com/logging.
Also, using document.write is not really elegant, you can use it only on page load, and it's blocking whole page. You basically should not use it at all. If you try to use document.write after page is loaded, it will replace whole content of your document with your last "log".