I have a JavaScript function code where I want to alert.
我有一个JavaScript功能代码,我想要提醒。
function msg(x,y)
{
tempstr = x.value
if(tempstr.length>y)
{
alert(c_AcknowledgementText);
x.value = tempstr.substring(0,y);
}
}
Now I have an xml with below format:
现在我有一个格式如下的xml:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<key name="c_ContactUsHeading">Contact Us</key>
<key name="c_AcknowledgementText">Comments can not be more than 250 characters.</key>
</root>
I want the JavaScript code so that the message shown in above alert can be read from above xml key name "c_AcknowledgementText".
我想要JavaScript代码,以便可以从上面的xml密钥名称“c_AcknowledgementText”中读取上面警告中显示的消息。
I hope it is clear about my problem.
我希望我的问题很清楚。
3 个解决方案
#1
Basically, you want to use XMLHttpRequest. Not sure what you're trying to do with tempstr, etc., though.
基本上,您想要使用XMLHttpRequest。不知道你正在尝试用tempstr等做什么。
function msg(x,y)
{
tempstr = x.value;
if(tempstr.length>y)
{
var req = new XMLHttpRequest();
req.open('GET', '/file.xml', true);
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200)
{
var keys = req.responseXML.getElementsByTagName("key");
for(var i = 0; i < keys.length; i++)
{
var key = keys[i];
if(key.getAttribute("name") == "c_AcknowledgementText")
{
alert(key.textContent);
break;
}
}
}
else
alert("Error loading page\n");
}
};
req.send(null);
x.value = tempstr.substring(0,y);
}
}
#2
First step is to get a DOM reference to the XML document. One way to do that is with Ajax. In this case, the server needs to respond with the Content-Type: text/xml header.
第一步是获取XML文档的DOM引用。一种方法是使用Ajax。在这种情况下,服务器需要使用Content-Type:text / xml标头进行响应。
$.ajax({
type: "GET",
url: "/path/to/my.xml",
dataType: "xml",
success: function(doc){
var keys = doc.getElementsByTagName('key');
if (keys.length > 0) {
for (var i=0; i<keys.length; i++) {
if (keys[i].getAttribute('name') == 'c_AcknowledgementText') {
alert(keys[i].innerHTML);
}
}
}
}
});
You may need some additional error-handling, etc.
您可能需要一些额外的错误处理等。
#3
You have to parse the XML file for c_AcknowledgementText (either in JavaScript or using a server side language and storing it into JavaScript as the page loads).
您必须为c_AcknowledgementText解析XML文件(使用JavaScript或使用服务器端语言并在页面加载时将其存储到JavaScript中)。
#1
Basically, you want to use XMLHttpRequest. Not sure what you're trying to do with tempstr, etc., though.
基本上,您想要使用XMLHttpRequest。不知道你正在尝试用tempstr等做什么。
function msg(x,y)
{
tempstr = x.value;
if(tempstr.length>y)
{
var req = new XMLHttpRequest();
req.open('GET', '/file.xml', true);
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200)
{
var keys = req.responseXML.getElementsByTagName("key");
for(var i = 0; i < keys.length; i++)
{
var key = keys[i];
if(key.getAttribute("name") == "c_AcknowledgementText")
{
alert(key.textContent);
break;
}
}
}
else
alert("Error loading page\n");
}
};
req.send(null);
x.value = tempstr.substring(0,y);
}
}
#2
First step is to get a DOM reference to the XML document. One way to do that is with Ajax. In this case, the server needs to respond with the Content-Type: text/xml header.
第一步是获取XML文档的DOM引用。一种方法是使用Ajax。在这种情况下,服务器需要使用Content-Type:text / xml标头进行响应。
$.ajax({
type: "GET",
url: "/path/to/my.xml",
dataType: "xml",
success: function(doc){
var keys = doc.getElementsByTagName('key');
if (keys.length > 0) {
for (var i=0; i<keys.length; i++) {
if (keys[i].getAttribute('name') == 'c_AcknowledgementText') {
alert(keys[i].innerHTML);
}
}
}
}
});
You may need some additional error-handling, etc.
您可能需要一些额外的错误处理等。
#3
You have to parse the XML file for c_AcknowledgementText (either in JavaScript or using a server side language and storing it into JavaScript as the page loads).
您必须为c_AcknowledgementText解析XML文件(使用JavaScript或使用服务器端语言并在页面加载时将其存储到JavaScript中)。