Please help, I can't implement this javascript to my Blogger...
(function() {
var pre = document.getElementsByTagName('pre'),
pl = pre.length;
for (var i = 0; i < pl; i++) {
pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="cl"></span>';
var num = pre[i].innerHTML.split(/\n/).length;
for (var j = 0; j < num; j++) {
var line_num = pre[i].getElementsByTagName('span')[0];
line_num.innerHTML += '<span>' + (j + 1) + '</span>';
}
}
})();
You can see this Javascript work fine here : http://jsfiddle.net/tovic/AbpRD/1/
If you are seeing that following type of error when you try to add this JavaScript snippet in your theme code -
Error parsing XML: The content of elements must consist of well-formed character data or markup.
Then to resolve this error, use any of the following methods -
1. Wrap the code within a CDATA directive in the script tag. The code will look like -
<script>
//<![CDATA[
(function() {
var pre = document.getElementsByTagName('pre'),
pl = pre.length;
for (var i = 0; i < pl; i++) {
pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="cl"></span>';
var num = pre[i].innerHTML.split(/\n/).length;
for (var j = 0; j < num; j++) {
var line_num = pre[i].getElementsByTagName('span')[0];
line_num.innerHTML += '<span>' + (j + 1) + '</span>';
}
}
})();
//]]>
</script>
The only downside of this approach being that Blogger XML parser will ignore any data layout tags (like for example <data:blog.homepageUrl/>) within the CDATA directive. Rather than replacing them with their actual values, it will not interpret them and show them as is.
2. Escape the following characters in your code -
" is replaced with "
& is replaced with &
< is replaced with <
> is replaced with >
After escaping, the code should look like -
<script>
(function() {
var pre = document.getElementsByTagName('pre'),
pl = pre.length;
for (var i = 0; i & lt; pl; i++) {
pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="cl"></span>';
var num = pre[i].innerHTML.split(/\n/).length;
for (var j = 0; j & lt; num; j++) {
var line_num = pre[i].getElementsByTagName('span')[0];
line_num.innerHTML += '<span>' + (j + 1) + '</span>';
}
}
})();
</script>
The data layout tags will remain functional when following this method. Remember to not escape <> surrounding the data layout tag (aka <data:blog.homepageUrl/> will work but not <data:blog.homepageUrl/>)
3. If no data layout tags have to be included in the JavaScript. Then you can add it in an HTML/JavaScript gadget via the Layout tab instead of directly including it in the theme code.
How can i make ASCII send what is typed than code.
I mean if A is type on a keyboard the result on php should be the letter typed and not the Code
var ucode = function(s) {
var len = s.length;
var rs = "";
for ( var i = 0; i < len; i++) {
var k = s.substring(i, i + 1);
rs += "$" + (s.charCodeAt(i) + "1") + ";";
}
return rs;
};
I have a bit of code that requires printing underscores but to the line above it, how would i do this? I'm not sure how to print the underscore to the previous line, not much experience with javascript. thanks!
var landscape = function() {
var result = "";
var flat = function(size) {
for (var count = 0; count < size; count++)
result += "_";
};
var hill = function(size) {
result += " /";
for (var count = 0; count < size; count++)
result += ""+
"_";
result += " \\";
};
//BUILD SCRIPT
flat(3)
hill(4);
flat(6);
hill(1);
flat(1);
//END SCRIPT
return result;
it makes this ___ /____ \______ /_ \_`enter code here`enter code here`
and i want this
_____ ___
___/ \__/ \____/\_
You can keep track of the two lines separately and then concatenate them just before returning the result.
JS:
function landscape() {
var resultTop = '';
var resultBottom = '';
function hill(size) {
resultTop += ' ';
resultBottom += '/';
for (var i = 0; i < size; i++) {
resultTop += '_';
resultBottom += ' ';
}
resultTop += ' ';
resultBottom += '\\';
}
function flat(size) {
for (var i = 0; i < size; i++) {
resultTop += ' ';
resultBottom += '_';
}
}
flat(3);
hill(4);
flat(6);
hill(1);
flat(1);
var result = resultTop + '<br/>' + resultBottom;
return result;
}
Here's a fiddle.
One workaround is to print a unicode character that draws a line on top. Turns out there is such a character: the Upper One-eighth Block
It's "\u2594" in unicode escape or ▔ as HTML entity or you can simply copy/paste the literal character from the example below:
____/▔▔▔▔\____/▔▔\___
I have a string that I have created where I would like to remove the last comma and replace it with a period. I want to keep all of the other commas. Here I was trying to use a conditional statement, it works to add commas, but it doesn't work to replace the last one with a period. I am new at this, I would really appreciate any help.
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
likesString = petObj.name + " " + " is a " + petObj.type + " she " + " likes ";
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length) {
var likesString = likesString + petObj.likes[j] + ", ";
}
else if (j == petObj.likes.length) {
likesString.replace(", ", ".");
}
}
displayResult();
}
You seem to be doing it a hard way! Use the Arry's join() method to build the list.
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
likesString = petObj.name + " is a " + petObj.type + " she likes " +
petObj.likes.join(", ") + ".";
displayResult();
}
This will do it:
str.replace(/,([^,]*)$/,".$1")
The regular expression matches on a comma followed by any number of non-commas all the way to the end of the string... by definition, this is the last comma. It works if there are no commas, one comma or any number of commas.
Notice that the 'if' part of this has to always be true so the 'else' part is never executed:
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length) {
var likesString = likesString + petObj.likes[j] + ", ";
}
else if (j == petObj.likes.length) {
likesString.replace(", ", ".");
}
}
The for loop says to only continue if that condition is true and your code then tests the same condition.
You might just do something like this:
for (var j = 0; j < petObj.likes.length; j++) {
if (j < petObj.likes.length-1) {
likesString = likesString + petObj.likes[j] + ", ";
}
else {
likesString = likesString + petObj.likes[j] + ". ";
}
}
There are better ways to do the condition that don't duplicate so much but that might do what you want. (Also I fixed the extra 'var' part.)
Here is a great place to learn and test regular expressions: RegExr
Essentially, you want to replace the following:
RegExp Pattern: /,([^,]+)$/
Replace Pattern: .$1
So your code should look like:
s.replace(/,([^,]+)$/, '.$1');
Where s is the string you're trying to replace the last comma in.
Don't forget to set that line to a variable to save it.
To replace the last occurrence of a comma in a string with a period, you can use:
var index = str.lastIndexOf(",");
var newstr = str.substring(0, index) + "." + str.substring(index + 1);
After looking at your code, it seems epascarello's approach is the best. In addition to what he has pointed out, if there are no elements in the array, your string will somewhat abruptly end with: "she likes". To fix this, you could use:
likesString = petObj.name + " is a " + petObj.type + (petObj.likes.length ? ", she likes " + petObj.likes.join(", ") : "") + ".";
For start you can avoid to put the last comma with a code like this:
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
var likesString = petObj.name + " " + " is a " + petObj.type;
if (petObj.likes.length) {
likesString = likesString + " she " + " likes " petObj.likes.join(", ");
}
likesString = likesString + '.'
displayResult();
}
If you still want to use your code you have to test versus lenght-1 as the likes array indexes go from 0 to length-1:
for (var i = 0; i < petArray.length; i++) {
petObj = petArray[i];
var likesString = petObj.name + " " + " is a " + petObj.type + " she " + " likes ";
for (var j = 0; j < petObj.likes.length; j++) {
likesString = likesString + petObj.likes[j] + ((j == petObj.likes.length-1)?'.':', ';
}
displayResult();
}
If you still want to change the string after its construction you can just remove the last two characters
likesString=likesString.replace(/, $/,'.')
That is because replace() returns a new string with the new value instead of changing the string itself, you could do this instead:
likesString = likesString.replace(/,\s$/, ".");
how would i add a prefix per line to a text area.. example:
this is the content of the textarea:
hello124
and i would want to add a [b] prefix and suffix to each line so that when i click a button the result will be:
[b]hello[/b]
[b]124[/b]
please help me :(
text = document.getElementById("the_textarea").value;
document.getElementById("the_textarea").value = text.replace(/.+/g, "[b]$&[/b]");
Example:
using joins and splits:
var prefix = '[b]', suffix = '[/b]',
txt = document.getElementById('myText');
txt.value = prefix + txt.value.split('\n').join(suffix + '\n' + prefix) + suffix;
Using only plain Javascript, you can do:
var textArea = document.getElementById("yourTextAreaID");
var lines = textArea.value.split("\n");
for (var i = 0; i < lines.length; ++i) {
lines[i] = "[b]" + lines[i] + "[/b]";
}
textArea.value = lines.join("\n");
Or, as #Alin Purcaru suggested, without using a loop:
var textArea = document.getElementById("yourTextAreaID");
textArea.value = "[b]" + textArea.value.split("\n").join("[/b]\n[b]") + "[/b]";
<script language="javascript" type="text/javascript">
function TextDefine(val){
var i= 0;
var array1 = val.value.split("\n");
for ( i = 0; i < array1.length; i++) {
array1[i] = "[b]" + array1[i] + "[/b]";
}
val.value = array1.join("\n");
}
</script>
<textarea name="data" id="data"></textarea>
<input type="button" name="submit1" value="Submit" onclick="TextDefine(document.getElementById('data'))" />
Convert the string into an array starting at position 1 of the array.
Push the prefix at position 0 of the array.
Convert the array back to a string with join.