我尝试了很多不同的方法,但没有运气。我要做的就是确定当前URL的域,并从基于域的XML文件中提取一个值。
任何帮助都会非常感激。
merchants.xml
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<merchant id="1">
<domain>http://www.amazon.com</domain>
<affiliate>1</affiliate>
</merchant>
</catalog>
popup.js
function ReadXML() {
try {
var xmlPath = "merchants.xml";
$.ajax({
type: "GET",
url: xmlPath,
dataType: "xml",
success: parseXML
});
} catch (e) {
alert("Error while reading XML; Description – " + e.description);
}
}
function parseXML(xml) {
var $merchant = $(xml).find('domain').filter(function() {
return $(this).text() == "http://www.amazon.com";
}).closest('domain');
var affiliate = $('affiliate', $domain).text();
if(window.location.hostname.indexOf("http://www.amazon.com") > -1) {
document.getElementById("demo").innerHTML = affiliate;
}
}
readXML()
popup.html
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style>
body {
font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
font-size: 75%;
}
#status {
/* avoid an excessively wide status text */
white-space: normal;
text-align: center;
width: 200px;
height: 225px;
overflow: hidden;
max-width: 400px;
word-wrap: normal;
}
</style>
<script type="text/javascript" src="popup.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<table id="demo"></table>
<div id="status"></div>
</body>
</html>
答案 0 :(得分:1)
您可以使用jQuery XML to JSON Plugin将XML转换为JSON。因此,您将获得包含所有属性的Javascript对象:
{"merchant":{"domain":"http://www.amazon.com","affiliate":"1","id":"1"}}
完整样本:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="https://jquery-xml2json-plugin.googlecode.com/svn/trunk/jquery.xml2json.js"></script>
<script type="text/javascript">
$(function() {
var
xml,
json;
xml =
'<?xml version="1.0" encoding="utf-8"?>' +
'<catalog>' +
'<merchant id="1">' +
'<domain>http://www.amazon.com</domain>' +
'<affiliate>1</affiliate>' +
'</merchant>' +
'</catalog>';
json = $.xml2json(xml);
alert(json.merchant.domain);
});
</script>
答案 1 :(得分:0)
var affiliateData = {};
function loadUrlData(url, data, affiliateData) {
var urlDomain = url.substring(url.indexOf('//') + 2);
if (urlDomain.indexOf('/') > -1) {
urlDomain = urlDomain.substring(0, urlDomain.indexOf('/'));
}
if (urlDomain.indexOf('?') > -1) {
urlDomain = urlDomain.substring(0, urlDomain.indexOf('?'));
}
lastIndexOfDot = urlDomain.lastIndexOf('.');
if (lastIndexOfDot < 0) {
return;
}
do {
affiliateData[urlDomain] = data;
urlDomain = urlDomain.slice(urlDomain.indexOf('.') + 1);
} while (urlDomain.indexOf('.') > -1);
};
function getAffiliateData(url, affiliateData) {
var urlDomain = url.substring(url.indexOf('//') + 2);
if (urlDomain.indexOf('/') > -1) {
urlDomain = urlDomain.substring(0, urlDomain.indexOf('/'));
}
if (urlDomain.indexOf('?') > -1) {
urlDomain = urlDomain.substring(0, urlDomain.indexOf('?'));
}
lastIndexOfDot = urlDomain.lastIndexOf('.');
if (lastIndexOfDot < 0) {
return 'none';
}
do {
for (domain in affiliateData) {
if (domain == urlDomain) {
return affiliateData[domain];
}
}
urlDomain = urlDomain.slice(urlDomain.indexOf('.') + 1);
} while (urlDomain.indexOf('.') > -1);
return 'none';
};
var url = 'http://www.amazon.com?asdf=/a/b/c';
var url2 = 'http://www.yahoo.com?asdf=/a/b/c';
loadUrlData('http://www.amazon.com?asdf=/a/b/c', 'data amazon', affiliateData);
loadUrlData('http://www.yahoo.com?asdf=/a/b/c', 'data yahoo', affiliateData);
alert(getAffiliateData(url, affiliateData));
alert(getAffiliateData(url2, affiliateData));