2
我嘗試了許多不同的方法,但沒有運氣。我所要做的就是確定當前URL的域,並從基於域的XML文件中提取一個值。在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>