CreateTextNode And Entities
Feb 25, 2006
Modified with a newer version since it was first posted. Some comments may refer to older versions.
function entity(str, mode) {
str = (str) ? str : "";
mode = (mode) ? mode : "string";
var e = document.createElement("div");
e.innerHTML = str;
if (mode == "numeric") {
return "&#" + e.innerHTML.charCodeAt(0) + ";";
}
else if (mode == "utf16") {
var un = e.innerHTML.charCodeAt(0).toString(16);
while (un.length < 4) un = "0" + un;
return "u" + un;
}
else return e.innerHTML;
}
entity() has two parameters:
- entity: is a string which can be either a named entity (»), numeric entity (»), UTF-16 value (u00bb), or even the character itself (»).
- mode: is an optional value that can be 'string', 'numeric', or 'utf16'. This tells the function what to return the value as. Defaults to 'string'.
You'd use it like this:
// String mode
var div = document.createElement('div');
var text = document.createTextNode('Parent '+entity('»')+' Child');
div.appendChild(text);
// Numeric mode
var description = "The entity for » is "+entity('»', 'numeric');
// UTF-16 mode
var description = "The UTF-16 value for » is "+entity('»', 'utf16');
View 3 Replies
Nov 30, 2006
When running this code in IE7:
function insertScript() {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
document.getElementById("myDiv").appendChild(newScript);
}
window.onload=insertScript;
I get this error:
Unexpected call to method or property access
And a pointer that points to the newScript.appendChild(s) line.
Am I using createTextNode incorrectly or is IE7 getting it wrong?
The function, as written, works correctly in FF2.0, Opera9 and AIUI,
Safari1.3.2 Its an attempt to get around Safari not supporting the
setting of the .text property of a script element. If IE7 simply won't
create the text and append it then feature testing for createTextNode
won't work. So, I came up with the idea of attempting to set the .text
property with a variable definition then reading that variable. If it is
set, then use the .text property. If it isn't set, then use
createTextNode. Not sure how reliable it is so I thought about using an
IE conditional to isolate IE and go based on that but it reeks of
browser detection.
View 50 Replies
View Related
Sep 22, 2005
Is there a way to get © to translate when using createTextNode ?
div.appendChild(document.createTextNode('Copyright © 1998-'+(new Date().getFullYear())+', SomeCompany'));
View 2 Replies
View Related
Jul 23, 2005
I would like to set the value of a hidden input field to the value of
a javascript variable.
In HTML I have successfully done this by:
<input type="hidden" name="context" value="&{exists};"/>
However I now wish to put this into an xsl file and it complains about
the { characters.
If I change them so that I have:
<input type="hidden" name="context" value="‹exists›"/>
Then I get an error along the lines of the entity lsaquo was
referenced, but not declared.
View 2 Replies
View Related
Nov 22, 2006
I need some help with Javascript and HTML entities. I am writing some code and I need to use quite a few HTML entities on alert boxes (messages in different languages)
Unfortunately Javascript displays the entities and not the equivalent caracther.
View 10 Replies
View Related
Feb 17, 2006
How can I convert some html entities (polish) to a character which I can use in a javascript alert?
The entities are:
ń
ę
ń
ą
ż
ś
ć
View 2 Replies
View Related
Jul 20, 2005
Using my IE v.6 browser, document.write doesn't convert HTML entities
(e.g. ', &) to the appropriate character (though NS 6.2 works
fine).
Obviously I can get round this for particular entities by writing some
code to do the conversion before using document.write - but I need a
more general solution that will catch any of the HTML entities.
A trawl with Google has found a number of people raising the question,
but no answers. Any suggestions?
View 4 Replies
View Related
Sep 22, 2011
This code was working fine until I added "["lastname"]["email"]" to it. Why has this problem developed? Was I suppose to separate them or something?
<SCRIPT type="text/javascript">
function validateForm() {
var x=document.forms["form"]["firstname"]["lastname"]["email"].value;
[code]....
View 2 Replies
View Related
Jun 18, 2010
I have a large set of input fields (of checkboxes and radio buttons), and aside each set, is a clear button (just looks like text you can click on) that clears any input next to each of the input fields. When no input buttons are selected, the HTML entities that depend on them are supposed to disappear, and this works on load.
My question is how does one easily incorporate that clear button so that it tells the listeners already in action to clear those entities when they click on clear, and without having to manually do it over again with the clear button, if that makes any sense.
[Code]...
View 2 Replies
View Related
Jan 27, 2010
I'm trying to write/find a regular expression for finding ampersands but not HTML entites.I have this which finds entities but can't figure out how to ignore entities and return unmatched "&"
&[^s]*;
Test string: ThisĀ is sample test containing a bunch of & and entities. Do you shop at: M&S? &x#1234;
I want to HTML encode the non-entity ampersands for insertion into XML e.g.
"bunch of & and" --> "bunch of & and"
View 9 Replies
View Related
Mar 14, 2007
I need to figure out a pattern that can match each letter of the message, but leaves all the html entities alone.
For example, I have a input like this:
<div>
This is the content < Hello >
</div>
Just as an example to make it more clearer, If I wanted to replace the all letters of the message with the number "1" I would have this result:
<div>
1111 11 111 1111111 < 11111 >
</div>
View 2 Replies
View Related
Jun 11, 2010
I am using the ajax method in JQuery to return an XML response from a URL location. The method looks like the following:
$.ajax({
url: '../DSMO/lb.asmx/GetAvailableDocListAuth',
cache: false,
type: 'POST',
[Code].....
OK so if I use repsonseText responseXML or just try and traverse msg it will not work. The problem is the body of my XML response, all of the tags ('<' and '>') are getting escaped to html entities ('<' and '>') So it becomes a malformed XML document that is not readable. I even tried using the Javascript function replace to go through and replace with no luck.
Even stranger if I access the web service through the browser, the XML is just fine!
The server is Windows Server 2008 running IIS 7 and I am programming of course in HTML and newest version of JQuery.
The browser I am testing on is Firefox with FireBug.
View 5 Replies
View Related